logback 添加自定义字段(转换)
<configuration scan="true"> <conversionRule conversionWord="serverIP" converterClass="com.foo.MyCustomTagConverter" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder --> <encoder> <pattern>%serverIP- %m%n</pattern> </encoder> </appender> ... </configuration>
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; 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; } }