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
類使用?
好的,很酷。那是有道理的! – AgentX