当我偶然发现slf4j code类中的bind()
方法
LoggerFactory
以了解绑定过程
感兴趣的代码段:
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
类?
答案 0 :(得分:1)
好吧,StaticLoggerBinder在slf4j-api
的源代码中,但它被删除,所以它不会在jar中结束。如果您下载JAR,您将看到它不存在。