您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。