In this post,we will see about SLF4J: Class Path Contains Multiple SLF4J Bindings.
Table of Contents
Using maven
If you are looking for quick solution for this issue, you need to find out how log4j is present on your path.
run mvn dependency:tree
and figure out the maven dependency and exclude log4j
with snippet below to that dependency in your pom.xml
.
1 2 3 4 5 6 7 8 |
<exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> |
This should resolve SLF4J: Class Path Contains Multiple SLF4J Bindings.
Using gradle
If you are using gradle, you need to use following code to exclude following dependency.
1 2 3 4 5 |
configurations.all { exclude module: 'slf4j-log4j12' } |
I have provided example below to figure out the dependency from which you should exclude log4j
dependency.
Understand the warning
Let’s first understand the warning.
SLF4J: Found binding in [jar:file:/Users/apple/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/Users/apple/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.1/log4j-slf4j-impl-2.13.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
It simply says ClassPath contains multiple SLF4J bindings i.e logback-classic-1.2.3.jar
and log4j-slf4j-impl-2.13.1.jar
.
To understand the warning, we need to understand about SLF4J
.
The Simple Logging Facade for Java (SLF4J)
provides simple abstraction or facade for various logging frameworks such as log4j, logback, and java.util.logging etc. and it allows the end-user to plug-in the desired logging framework at deployment time.
SLF4J looks for bindings on the classpath to plugin logging framework. If more than one binding is present on the classpath, it will give a warning.
Please note that this is just a warning, not an error, it will pick one of the binding and will go ahead with it.
For example:
SLF4J
has chosen logback
in above warning. You can have a look at this line.
How to resolve SLF4J: Class-Path Contains Multiple SLF4J Bindings?
We need to find a conflicting jars to find root cause of the warning.
You can use the following command to trace the conflicting jar.
1 2 3 |
mvn dependency:tree |
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.2.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.2.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] \- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.13.1:compile
As you can see, we have two slf4j bindings here.
- ch.qos.logback:logback-classic:jar
- org.apache.logging.log4j:log4j-slf4j-impl:jar
logback-classic
is transitive dependency, fetched due to spring-boot-starter-web
. We have added explicitly log4j-slf4j-impl
to use log4j in our project.
To avoid this warning, we need to exclude the unwanted dependencies in pom.xml
.
In this example, I have excluded logback-classic
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> </exclusion> </exclusions> </dependency> |
Update the maven project and run the application. You should not get SLF4J Warning: Class Path Contains Multiple SLF4J Bindings
now.
Reference
SLF4J: class path contains multiple SLF4J bindings.
That’s all about SLF4J: Class Path Contains Multiple SLF4J Bindings.