Kazu's Log

Mar 29, 2016

How does SLF4J know about multiple logger implementations?

One day, I got a small warning from SLF4J.

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [...!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [...!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

This was because I had multiple SLF4J implementations in the classpath. So I cleaned up the dependencies to reduce the implementations. Problem solved.

But how does SLF4J know about multiple logger implementations?

LoggerFactory to StaticLoggerBinder

LoggerFactory.getLogger(Class<?>) is the main facade of SLF4J. It ultimately calls bind() to initialize StaticLoggerBinder.

Here is the trick. All SLF4J implementations provide own StaticLoggerBinder. They simply define org.slf4j.impl.StaticLoggerBinder class and it is perfectly fine in Java world when those implementations are in different jar files.

findPossibleStaticLoggerBinderPathSet() is the method that uses ClassLoader to find a possible implementation of the class. The result array is passed to reportMultipleBindingAmbiguity() that shows the warning.

slf4j-api’s StaticLoggerBinder

slf4j-api also defines StaticLoggerBinder as well, but it gets removed by pom.xml. So the jar doesn’t contain the class.