package com.demo.conf;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 配置日志中显示IP
*/
public class IPLogConfig extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
}
<!--配置规则类的位置-->
<conversionRule conversionWord="ip" converterClass="com.demo.conf.IPLogConfig" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%white(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%lsn) %white(%ip) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{10}) - %cyan(%msg%n)</pattern>
<charset>utf8</charset>
</encoder>
</appender>