How does slf4j bind to implementation? Does it really do so during compile time?

后端 未结 6 1704
轮回少年
轮回少年 2021-02-13 03:58

In the documentation for slf4j it says that the binding happens during complie time:

\"SLF4J does not rely on any special class loader machinery. In fact, each SLF4J bi

6条回答
  •  粉色の甜心
    2021-02-13 04:21

    Here is the source code of slf4j. Slf4j will find all the class in the class path whose path is "org/slf4j/impl/StaticLoggerBinder.class". And if there are more than one, jvm will pick up only one randomly.For more detail you can see here:http://www.slf4j.org/codes.html#multiple_bindings

    // We need to use the name of the StaticLoggerBinder class, but we can't
    // reference
    // the class itself.
    
    private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";
    
    static Set findPossibleStaticLoggerBinderPathSet() {
     // use Set instead of list in order to deal with bug #138
     // LinkedHashSet appropriate here because it preserves insertion order
     // during iteration
        Set staticLoggerBinderPathSet = new LinkedHashSet(); 
        try {
            ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
            Enumeration paths;
            if (loggerFactoryClassLoader == null) {
                paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);
            } else {
                paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH);
            }
            while (paths.hasMoreElements()) {
                URL path = paths.nextElement();
                staticLoggerBinderPathSet.add(path);
            }
        } catch (IOException ioe) {
            Util.report("Error getting resources from path", ioe);
        }
        return staticLoggerBinderPathSet;
    }
    

提交回复
热议问题