目录logback日志配置获取服务器ip和端口1、新建一个类继承ClassicConverter2、在配置文件logback.xml增加如下配置3、在需要用到ip的位置这样写: %i
在方法中获取服务器ip
package com.xxx.xxx.xxx.common;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LogIpConfig extends ClassicConverter {
private static final Logger logger = LoggerFactory.getLogger(LogIpConfig .class);
private static String WEBIP;
static {
try {
webIP = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.error("获取日志Ip异常", e);
webIP = null;
}
}
@Override
public String convert(ILoggingEvent event) {
return webIP;
}
}
<conversionRule conversionWord="ip" converterClass="com.xxx.xxx.xxx.common.LogIpConfig" />
"host": "%ip" 这样写,本地日志输入的时候内容是: "host": "127.0.0.1"
package com.xxx.xxx.xxx.common;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class LogPortConfig extends ClassicConverter {
private static final Logger logger = LoggerFactory.getLogger(LogPortConfig.class);
private static String webPort;
static {
try {
List<MBeanServer> serverList = MBeanServerFactory.findMBeanServer(null);
for (MBeanServer server : serverList) {
Set<ObjectName> names = new HashSet<ObjectName>();
names.addAll(server.queryNames(new ObjectName("Catalina:type=Connector,*"), null));
Iterator<ObjectName> it = names.iterator();
while (it.hasNext()) {
ObjectName oName = (ObjectName) it.next();
String pValue = (String) server.getAttribute(oName, "protocol");
if (StringUtils.equals("Http/1.1", pValue)) {
webPort = ObjectUtils.toString(server.getAttribute(oName, "port"));
}
}
}
} catch (Exception e) {
logger.error("获取port失败,影响logback的文件拼接", e);
webPort = null;
}
}
@Override
public String convert(ILoggingEvent event) {
return webPort;
}
}
<conversionRule conversionWord="port" converterClass="com.xxx.xxx.xxx.common.LogPortConfig" />
%ip:%port
一个类继承自ch.qos.logback.classic.pattern.ClassicConverter
@Override
public String convert(ILoggingEvent event) {}
<conversionRule conversionWord="sulong" converterClass="SulonGClass" />
<!--配置日志的格式-->
<property name="CONSOLE_LOG_PATTERN" value="%sulong %date{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %yellow(%thread) | %green(%logger) | %msg%n"/>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 如何在logback日志配置里获取服务器ip和端口
本文链接: https://www.lsjlt.com/news/133977.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-11
2024-05-11
2024-05-10
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0