需求:在做日志采集时需要服务器ip,于是将服务器ip打印在日志中
这里使用了logback配置
首先,创建一个java文件,用来扩展日志字段address:
package com.jason.monitor.config;
import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class AddressConverter extends MessageConverter {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private static String IP_ADDRESS = "";
/**
* 每次打印日志,该方法都会被调用
* @param event
* @return
*/
@Override
public String convert(ILoggingEvent event) {
if (StringUtils.isEmpty(IP_ADDRESS)) {
Set<String> ips = this.getLinuxLocalIp();
logger.debug("获取IP地址:{}", ips.stream().collect(Collectors.joining(",")));
Optional<String> result = ips.stream().findFirst();
IP_ADDRESS = result.orElse("127.0.0.1");
}
return IP_ADDRESS;
}
/**
* 获取IP地址
* @return IP地址
* @throws SocketException
*/ private Set<String> getLinuxLocalIp() {
Set<String> ips = new HashSet<>();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
ips.add(ipaddress);
}
}
}
}
}
} catch (SocketException e) {
logger.info("获取IP地址失败:{}", e.getMessage());
}
return ips;
}
}
然后,在logback-spring.xml中加入如下部分:
<!-- 打印IP地址 -->
<conversionRule conversionWord="address" converterClass="com.jason.monitor.config.AddressConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(MONITOR) ${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%address]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
这里 MONITOR
是当前服务名,由于是一个固定字段,所以直接在格式中固定就行。
最后启动服务,看一下效果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。