2016-04-02 95 views
1

我正想通過slf4j code瞭解綁定過程,當我在LoggerFactory類偶然發現了bind()方法的興趣線如何SLF4J綁定日誌框架

代碼片段:

 if (!isAndroid()) { 
      staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet(); 
      reportMultipleBindingAmbiguity(staticLoggerBinderPathSet); 
     } 
     // the next line does the binding 
     StaticLoggerBinder.getSingleton(); 
     INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION; 
     reportActualBinding(staticLoggerBinderPathSet); 
     replayEvents(); 

方法findPossibleStaticLoggerBinderPathSet()是這樣的:

private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class"; 
try { 
     ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader(); 
     Enumeration<URL> paths; 
     if (loggerFactoryClassLoader == null) { 
      paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH); 
     } else { 
      paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH); 
     } 

由於默認slf4j-api包提供了一個名稱爲StaticLoggerBinder的類,其中包含一些默認值,取決於綁定項目提供的實際實現(log4j,logback等)。

它究竟如何識別哪個StaticLoggerBinder類使用?

回答

1

好吧,StaticLoggerBinder位於slf4j-api的源代碼中,但它被刪除,因此它不會在jar中結束。如果你下載JAR,你會發現它不在那裏。

+0

好的,很酷。那是有道理的! – AgentX