Flutter开发App的未来及其在各行业的应用潜力分析
2060
2022-12-14
如何在logback日志配置里获取服务器ip和端口
目录logback日志配置获取服务器ip和端口1、新建一个类继承ClassicConverter2、在配置文件logback.xml增加如下配置3、在需要用到ip的位置这样写: %ip4、获取端口号,同理让Logback日志中显示主机名与IP地址及一切你想显示的1、创建2、重新方法3、配置logback.xml
logback日志配置获取服务器ip和端口
1、新建一个类继承ClassicConverter
在方法中获取服务器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-.InetAddress;
import java-.UnknownHostException;
/**
* @author xiaoming
* @date 2019/5/14 11:37
* @description
*/
public class LogIpConfig extends ClassicConverter {
private static final Logger logger = LoggerFactory.getLogger(LogOCDrJoGHsXIpConfig .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;
}
}
2、在配置文件logback.xml增加如下配置
3、在需要用到ip的位置这样写: %ip
"host": "%ip" 这样写,本地日志输入的时候内容是: "host": "127.0.0.1"
4、获取端口号,同理
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;
/**
* @author xiaoming
* @date 2019/5/14 11:37
* @description
*/
public class LogPortConfig extends ClassicConverter {
private static final Logger logger = LoggerFactory.getLogger(LogPortConfig.class);
private static String webPort;
static {
try {
List
for (MBeanServer server : serverList) {
Set
names.addAll(server.queryNames(new ObjectName("Catalina:type=Connector,*"), null));
Iterator
while (it.hasNext()) {
ObjectName oName = (ObjectName) it.next();
String pValue = (String) server.getAttribute(oName, "protocol");
OCDrJoGHsX if (StringUtils.equals("HTTP/1.1", pValue)) {
webPort = ObjectUtils.toString(serhttp://ver.getAttribute(oName, "port"));
}
}
}
} catch (Exception e) {
logger.error("获取port失败,影响logback的文件拼接", e);
webPort = null;
}
}
@Override
public String convert(ILoOCDrJoGHsXggingEvent event) {
return webPort;
}
}
%ip:%port
让Logback日志中显示主机名与IP地址及一切你想显示的
1、创建
一个类继承自ch.qos.logback.classic.pattern.ClassicConverter
2、重新方法
@Override
public String convert(ILoggingEvent event) {}
3、配置logback.xml
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~