这篇文章主要介绍了怎么在logback日志配置里获取服务器ip和端口,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
在方法中获取服务器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; /** * @author xiaoming * @date 2019/5/14 11:37 * @description */ 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; /** * @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<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"/>
感谢你能够认真阅读完这篇文章,希望小编分享的“怎么在logback日志配置里获取服务器ip和端口”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。