Recently, I received these errors when working on a Java web app:
[ERROR] SLF4J: Class path contains multiple SLF4J bindings.
[ERROR] SLF4J: Found binding in [jar:file:{userDirectory}/WEB-INF/lib/log4j-slf4j-impl-2.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[ERROR] SLF4J: Found binding in [jar:file:{userDirectory}/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.2/log4j-slf4j-impl-2.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[ERROR] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[ERROR] SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
The offending dependency is the log4j-slf4j-impl
dependency. Here’s what this dependency looks like in my pom.xml:
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>{my_version_number}</version> </dependency>
This error simply means that there are two jars that are binding to SLF4J. The solution is trivial. You must make it so that only one jar can bind with SLF4J.
To fix this, change the scope
tag in your pom.xml to provided. This will require that the server that this app runs on provide this dependency. Here’s how we do this:
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>{my_version_number}</version> <!-- This scope tag prevents multiple bindings. It requires the server to provide the dependency at runtime. --> <scope>provided</scope> </dependency>
Now, rerun your app and you shouldn’t see those errors anymore.
This isn’t necessarily the ideal solution because now we have a dependency that isn’t managed Maven. In some cases, changing the scope
tag to provided is the only solution that works.
Another solution is to use the mvn dependency:tree
command to comb through your dependencies to see if a dependency is pulling in a log4j-slf4j-impl
jar. If so, utilize maven’s excludes
tag to exclude the log4j-slf4j-impl
from the associated dependency. I personally didn’t use this solution so I am not going to go into more detail but googling “maven exclude tags” should give you a place to start; I don’t want to give out information that I haven’t personally tested.
So, remember, the problem is you have two log4j-slf4j-impl
jars on your classpath. You need to make it so that only one log4j-slf4j-impl
jar is on your classpath.
I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.
some really marvelous work on behalf of the owner of this web site, utterly outstanding written content.