当我偶然发现类中的方法时,我正在通过slf4j代码来了解绑定过程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
要使用的类?
好吧,StaticLoggerBinder位于的源中slf4j-api
,但是已将其删除,因此不会最终出现在jar中。如果下载JAR,您会发现它不存在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句