slf4j如何绑定到Logging Framework

时间:2016-04-02 19:02:51

标签: logging slf4j

当我偶然发现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类?

1 个答案:

答案 0 :(得分:1)

好吧,StaticLoggerBinder在slf4j-api的源代码中,但它被删除,所以它不会在jar中结束。如果您下载JAR,您将看到它不存在。

相关问题