I was going through the slf4j code to understand the binding process when I stumbled upon the bind()
method in the LoggerFactory
class
Code snippet of lines of interest:
if (!isAndroid()) {
staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
}
// the next line does the binding
StaticLoggerBinder.getSingleton();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
reportActualBinding(staticLoggerBinderPathSet);
replayEvents();
The method findPossibleStaticLoggerBinderPathSet()
is something like this:
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);
}
Since the default slf4j-api
package provides a class with name StaticLoggerBinder
with some defaults and depends on the actual implementations provided by the Binding Projects (log4j, logback etc.).
How does it actually identify which StaticLoggerBinder
class to use?
Well, StaticLoggerBinder is in the source of slf4j-api
, but it is removed so it does not end up in the jar. If you download the JAR, you will see it is not there.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments