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