第步:org.slf4j.LoggerFactory#getLogger(java.lang.String)
public static Logger getLogger(String name) { ILoggerFactory iLoggerFactory = getILoggerFactory(); return iLoggerFactory.getLogger(name); }获取一个ILoggerFactory,即LoggerContext。然后从其获取到Logger对象。
第3步:org.slf4j.LoggerFactory#getILoggerFactory
public static ILoggerFactory getILoggerFactory() { return getProvider().getLoggerFactory(); }第4步:org.slf4j.LoggerFactory#getProvider
static SLF4JServiceProvider getProvider() { if (INITIALIZATION_STATE == UNINITIALIZED) { synchronized (LoggerFactory.class) { if (INITIALIZATION_STATE == UNINITIALIZED) { INITIALIZATION_STATE = ONGOING_INITIALIZATION; performInitialization(); } } } switch (INITIALIZATION_STATE) { case SUCCESSFUL_INITIALIZATION: return PROVIDER; case NOP_FALLBACK_INITIALIZATION: return NOP_FALLBACK_FACTORY; case FAILED_INITIALIZATION: throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG); case ONGOING_INITIALIZATION: return SUBST_PROVIDER; } throw new IllegalStateException("Unreachable code"); }对SLF4JServiceProvider初始化,即LogbackServiceProvider对象。然后检查初始化状态,如果成功就返回PROVIDER。
第5步:org.slf4j.LoggerFactory#performInitialization
private final static void performInitialization() { bind(); if (INITIALIZATION_STATE == SUCCESSFUL_INITIALIZATION) { versionSanityCheck(); } }第6步:org.slf4j.LoggerFactory#bind
private final static void bind() { try { // 加载 SLF4JServiceProvider List<SLF4JServiceProvider> providersList = findServiceProviders(); reportMultipleBindingAmbiguity(providersList); if (providersList != null && !providersList.isEmpty()) { PROVIDER = providersList.get(0); // SLF4JServiceProvider.initialize() PROVIDER.initialize(); INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION; reportActualBinding(providersList); fixSubstituteLoggers(); replayEvents(); SUBST_PROVIDER.getSubstituteLoggerFactory().clear(); } else { // 省略代码。。。 } } catch (Exception e) { // 失败,设置状态值,上报 failedBinding(e); throw new IllegalStateException("Unexpected initialization failure", e); } }通过ServiceLoader加载LogbackServiceProvider,然后进行初始化相关字段。初始化成功后把初始化状态设置成功状态值。
第7步:ch.qos.logback.classic.spi.LogbackServiceProvider#initialize
public void initialize() { // 初始化默认的loggerContext defaultLoggerContext = new LoggerContext(); defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME); initializeLoggerContext(); markerFactory = new BasicMarkerFactory(); mdcAdapter = new LogbackMDCAdapter(); }创建名字为default的LoggerContext对象,并初始化一些字段默认值。
ch.qos.logback.classic.LoggerContext#LoggerContext
public LoggerContext() { super(); this.loggerCache = new ConcurrentHashMap<String, Logger>(); this.loggerContextRemoteView = new LoggerContextVO(this); 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<String>(); }初始化LoggerContext时设置了ROOT的Logger,日志级别为DEBUG。
第8步:ch.qos.logback.classic.spi.LogbackServiceProvider#initializeLoggerContext
private void initializeLoggerContext() { try { try { new ContextInitializer(defaultLoggerContext).autoConfig(); } catch (JoranException je) { Util.report("Failed to auto configure default logger context", je); } // 省略代码。。。 } catch (Exception t) { // see LOGBACK-1159 Util.report("Failed to instantiate [" + LoggerContext.class.getName() + "]", t); } }把第7步初始化好的LoggerContext当做参数传入ContextInitializer,构建其对象。然后解析配置文件。
第9步:ch.qos.logback.classic.util.ContextInitializer#autoConfig
public void autoConfig() throws JoranException { StatusListenerConfigHelper.installIfAsked(loggerContext); // (1) 从指定路径获取 URL url = findURLOfDefaultConfigurationFile(true); if (url != null) { configureByResource(url); } else { // (2) 从运行环境中获取 Configurator c = EnvUtil.loadFromServiceLoader(Configurator.class); if (c != null) { // 省略代码。。。 } else { // (3)设置默认的 BasicConfigurator basicConfigurator = new BasicConfigurator(); basicConfigurator.setContext(loggerContext); basicConfigurator.configure(loggerContext); } } }