logback 通过javaconfig实现配置

创建JAVACONFIG

  • myapp.BasicConfigurator
public class BasicConfigurator
 extends ch.qos.logback.core.spi.ContextAwareBase
  implements ch.qos.logback.classic.spi.Configurator {

    public BasicConfigurator() {
    }

    public void configure(LoggerContext lc) {
        addInfo("Setting up default configuration.");
        
        ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
        ca.setContext(lc);
        ca.setName("console");
        LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
        encoder.setContext(lc);
        
 
        // same as 
        // PatternLayout layout = new PatternLayout();
        // layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
        TTLLLayout layout = new TTLLLayout();
 
        layout.setContext(lc);
        layout.start();
        encoder.setLayout(layout);
        
        ca.setEncoder(encoder);
        ca.start();
        
        Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
        rootLogger.addAppender(ca);
    }
}

创建SPI配置文件

创建文件 META-INF\services\ch.qos.logback.classic.spi.Configurator

# 指定自定义javaconfig实现类
myapp.BasicConfigurator 

注意此方法在springboot应用中无效

springboot logback配置源码

springboot配置方法

springboot通过javaconfig实现logback配置

posted on 2022-04-11 22:39  路过君  阅读(139)  评论(0编辑  收藏  举报

导航