需要:在做日志采集时须要服务器 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
是以后服务名,因为是一个固定字段,所以间接在格局中固定就行。
最初启动服务,看一下成果: