首页   快速返回

logback初始化过程     所属分类 java
logback 1.2.3

logback-classic 
org.slf4j.impl.StaticLoggerBinder
slf4j 静态绑定

SINGLETON = new StaticLoggerBinder();
SINGLETON.init();

初始化 LoggerContext (ILoggerFactory)

public class LoggerContext extends ContextBase implements ILoggerFactory, LifeCycle



new ContextInitializer(defaultLoggerContext).autoConfig();


ch.qos.logback.classic.util.ContextInitializer


 public void autoConfig() throws JoranException {
        StatusListenerConfigHelper.installIfAsked(loggerContext);
        URL url = findURLOfDefaultConfigurationFile(true);
        if (url != null) {
            configureByResource(url);
        } else {
            Configurator c = EnvUtil.loadFromServiceLoader(Configurator.class);
            if (c != null) {
                try {
                    c.setContext(loggerContext);
                    c.configure(loggerContext);
                } catch (Exception e) {
                    throw new LogbackException(String.format("Failed to initialize Configurator: %s using ServiceLoader", c != null ? c.getClass()
                                    .getCanonicalName() : "null"), e);
                }
            } else {
                BasicConfigurator basicConfigurator = new BasicConfigurator();
                basicConfigurator.setContext(loggerContext);
                basicConfigurator.configure(loggerContext);
            }
        }
    }


findURLOfDefaultConfigurationFile
    findConfigFileURLFromSystemProperties
    getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus);
    getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus);
    getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus);
EnvUtil.loadFromServiceLoader
BasicConfigurator


配置加载顺序

1. findConfigFileURLFromSystemProperties 
   logback.configurationFile属性
   java -Dlogback.configurationFile=/path/to/mylogback.xml
2. logback-test.xml
3. logback.groovy
4. logback.xml
5. EnvUtil.loadFromServiceLoader
6. BasicConfigurator 控制台输出
 

public void configure(LoggerContext lc)




public class BasicConfigurator extends ContextAwareBase implements Configurator {

    public BasicConfigurator() {
    }

    public void configure(LoggerContext lc) {
        addInfo("Setting up default configuration.");
        
        ConsoleAppender ca = new ConsoleAppender();
        ca.setContext(lc);
        ca.setName("console");
        LayoutWrappingEncoder encoder = new LayoutWrappingEncoder();
        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);
    }
}




// xml groovy
configureByResource(URL url)

xml格式 

JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);


      
LoggerContext
LoggerContextVO

LoggerContextVO offers a restricted view of LoggerContext intended to be exposed by LoggingEvent to remote systems. 
This restricted view is optimized for serialization.

Map loggerCache = new ConcurrentHashMap();

 public LoggerContext() {
        super();
        this.loggerCache = new ConcurrentHashMap();

        this.loggerContextRemoteView = new LoggerContextVO(this);
        // ROOT
        this.root = new Logger(Logger.ROOT_LOGGER_NAME, null, this);
        this.root.setLevel(Level.DEBUG);
        loggerCache.put(Logger.ROOT_LOGGER_NAME, root);
        initEvaluatorMap();
        size = 1;
        this.frameworkPackages = new ArrayList();
    }

上一篇     下一篇
jdk日志无缝迁移到slf4j

常用时间单位换算

slf4j简介

logback的内部日志输出

System.out重定向到slf4j

各种日志重定向到slf4j