logback日志配置獲取服務(wù)器ip和端口
1、新建一個(gè)類(lèi)繼承ClassicConverter
在方法中獲取服務(wù)器ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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; } } |
2、在配置文件logback.xml增加如下配置
1
|
< conversionRule conversionWord = "ip" converterClass = "com.xxx.xxx.xxx.common.LogIpConfig" /> |
3、在需要用到ip的位置這樣寫(xiě): %ip
"host": "%ip" 這樣寫(xiě),本地日志輸入的時(shí)候內(nèi)容是: "host": "127.0.0.1"
4、獲取端口號(hào),同理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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; } } |
1
|
< conversionRule conversionWord = "port" converterClass = "com.xxx.xxx.xxx.common.LogPortConfig" /> |
%ip:%port
讓Logback日志中顯示主機(jī)名與IP地址及一切你想顯示的
1、創(chuàng)建
一個(gè)類(lèi)繼承自ch.qos.logback.classic.pattern.ClassicConverter
2、重新方法
1
2
|
@Override public String convert(ILoggingEvent event) {} |
3、配置logback.xml
1
2
3
|
< 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" /> |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/ming451294498/article/details/90205195