Reputation: 6186
I'm getting the following error. It seems there are multiple logging frameworks bound to slf4j. Not sure how to resolve this. Any help is greatly appreciated.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/admin/.m2/repository/org/slf4j/slf4j-log4j12/1.6.4/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/admin/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Upvotes: 314
Views: 568589
Reputation: 540
The dependency exclusion approach, for Gradle, in Kotlin DSL (*.gradle.kts):
configurations.all { exclude(group = "org.slf4j", module = "slf4j-log4j12") }
Upvotes: 0
Reputation: 61
Adding these to my gradle file fixed my issues
configurations.configureEach {
exclude group: 'org.slf4j', module: 'slf4j-reload4j'
exclude group: 'org.slf4j', module: 'slf4j-simple'
}
Upvotes: 0
Reputation: 5326
I've a similar problem but using maven-surefire-plugin
:
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-api ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
SLF4J: Class path contains multiple
SLF4J providers. SLF4J: Found provider [org.slf4j.nop.NOPServiceProvider@2a32d6c]
SLF4J: Found provider [org.slf4j.jul.JULServiceProvider@7692d9cc]
SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual provider is of type [org.slf4j.nop.NOPServiceProvider@2a32de6c]
And after run mvn dependency:tree
I find the problem:
[INFO] +- org.apache.maven.plugins:maven-surefire-plugin:jar:2.22.2:test
[INFO] | \- org.apache.maven.surefire:maven-surefire-common:jar:2.22.2:test
[INFO] | +- org.apache.maven:maven-plugin-api:jar:2.2.1:test
[INFO] | +- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.5.2:test
[INFO] | +- org.apache.maven.surefire:surefire-api:jar:2.22.2:test
[INFO] | | \- org.apache.maven.surefire:surefire-logger-api:jar:2.22.2:test
[INFO] ...
[INFO] | | +- org.apache.maven.wagon:wagon-webdav-jackrabbit:jar:1.0-beta-6:test
[INFO] | | | +- org.apache.jackrabbit:jackrabbit-webdav:jar:1.5.0:test
[INFO] | | | | \- org.apache.jackrabbit:jackrabbit-jcr-commons:jar:1.5.0:test
[INFO] | | | \- org.slf4j:slf4j-nop:jar:2.0.7:test
[INFO] | | +- org.slf4j:slf4j-jdk14:jar:2.0.7:test
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:2.0.7:test
The package org.slf4j:slf4j-jdk14
is the cause and not slf4j-api
or slf4j-log4j12
.
After exclude slf4j-jdk14
dependency the problem is resolved:
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
[...]
</plugins>
Now the build is clean without warnings:
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-api ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running HexagonalTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] Time elapsed: 2.69 s - in MyTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ...
[INFO] -------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------
[INFO] Total time: 8.888 s
[INFO] Finished at: 2023-05-08T13:08:26-03:00
[INFO] -------------------------------------------------------
Upvotes: 0
Reputation: 3002
For all those looking for the solution for spring-boot
-type dependencies, the magic incantation for Gradle is this:
configurations.all {
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
in your build.gradle
at the top level (not inside the dependencies
block).
All other solutions found in the interwebs (including the one here suggesting to exclude the slf4j
module) did not work for me.
This is what I have in my build.gradle
(snippet):
// Removes the annoying warning about the multiple SLF4J implementations:
// SLF4J: Class path contains multiple SLF4J bindings.
configurations.all {
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
implementation ('org.springframework.boot:spring-boot-starter-actuator')
implementation ('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
implementation ('org.springframework.boot:spring-boot-starter-webflux')
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
compileOnly "org.projectlombok:lombok:${lombokVersion}"
// Removes the annoying warning:
// warning: unknown enum constant When.MAYBE
// reason: class file for javax.annotation.meta.When not found
// See: https://stackoverflow.com/questions/29805622/could-not-find-or-load-main-class-org-gradle-wrapper-gradlewrappermain/31622432
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
// other stuff...
YMMV
Upvotes: 1
Reputation: 1688
1.Finding the conflicting jar
If it's not possible to identify the dependency from the warning, then you can use the following command to identify the conflicting jar
mvn dependency: tree
This will display the dependency tree for the project and dependencies who have pulled in another binding with the slf4j-log4j12
JAR.
Now that we know the offending dependency, all that we need to do is exclude the slf4j-log4j12
JAR from that dependency.
Ex - if spring-security
dependency has also pulled in another binding with the slf4j-log4j12
JAR, Then we need to exclude the slf4j-log4j12
JAR from the spring-security
dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Note - In some cases multiple dependencies have pulled in binding with the slf4j-log4j12
JAR and you don't need to add exclude for each and every dependency that has pulled in.
You just have to do that add exclude dependency with the dependency which has been placed at first.
Ex -
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
If you work with gradle then add following code to your build.gradle
file to exclude SLF4J binding from all the modules
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
Upvotes: 9
Reputation: 382
In my case I had 2 sources of dependencies for log4 one in C:\Program Files\smcf.ear directory and the second from maven which caused the multiple binding for sl4j.
Deleting the smcf.ear directory solved the issue for me.
Upvotes: -1
Reputation: 343
In case these logs are the result of this fix: https://stackoverflow.com/a/9919375/2894819
When one of your libraries actually use it. And your application doesn't need SL4J just replace implementation to runtimeOnly.
// contains dependency to sl4j-api
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.2.0")
// add this to remove both warnings
runtimeOnly("org.slf4j:slf4j-nop:1.7.36")
In that case when you run your app the actual dependency will be included once by the library and won't be included to the bundle of your application.jar itself.
Upvotes: 0
Reputation: 561
I solved this by going to Project Structure from my Intellij project. I deleted the file named: Maven: org.apache.logging.log4j:log4j-to-slf4j-impl:2.14.1
This file is not shown in this picture. You may see two libraries mentioned as log4j-to-slf4j. Delete one and you are good to go.
Upvotes: 2
Reputation: 41
I got this issue in a non-maven project, two depended jar each contained a slf4j. I solved by remove one depended jar, compile the project(which of course getting failure) then add the removed one back.
Upvotes: 0
Reputation: 21
I had the same problem. In my pom.xml i had both
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.28</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
When i deleted the spring-boot-starter-web dependency, problem was solved.
Upvotes: -1
Reputation: 11881
The combination of <scope>provided</scope>
and <exclusions>
didn't work for me.
I had to use this:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/empty.jar</systemPath>
</dependency>
Where empty.jar
is a jar file with literally nothing in it.
Upvotes: -1
Reputation: 1922
This is issue because of StaticLoggerBinder.class class belongs to two different jars. this class references from logback-classic-1.2.3.jar and same class also referenced from log4j-slf4j-impl-2.10.0.jar. both of jar in classpath. Hence there is conflict between them. This is reason of log file is not generation even though log4j2.xml file in classpath [src/main/resource].
We have so select one of jar, I recommend use log4j-slf4j-impl-2.10.0.jar file and exclude logback-classic-1.2.3.jar file.
Solution: open pom file and view the dependency Hierarchy [eclipse] or run
mvn dependency:tree command to find out the dependency tree and source of dependency that download the dependency. find the conflicting dependency and exclude them. For Springboot application try this.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
This is working fine for me after struggling a lots.
Upvotes: 4
Reputation: 612
For me the answer was to force a Maven rebuild. In Eclipse:
Upvotes: 2
Reputation: 4775
The error probably gives more information like this (although your jar names could be different)
SLF4J: Found binding in [jar:file:/D:/Java/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:/D:/Java/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.8.2/log4j-slf4j-impl-2.8.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
Noticed that the conflict comes from two jars, named logback-classic-1.2.3
and log4j-slf4j-impl-2.8.2.jar
.
Run mvn dependency:tree
in this project pom.xml parent folder, giving:
Now choose the one you want to ignore (could consume a delicate endeavor I need more help on this)
I decided not to use the one imported from spring-boot-starter-data-jpa
(the top dependency) through spring-boot-starter
and through spring-boot-starter-logging
, pom becomes:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
in above pom spring-boot-starter-data-jpa
would use the spring-boot-starter
configured in the same file, which excludes logging
(it contains logback
)
Upvotes: 44
Reputation: 4667
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-log4j2</artifactId>-->
<!--</dependency>-->
I solved by delete this:spring-boot-starter-log4j2
Upvotes: 4
Reputation: 9497
For me, it turned out to be an Eclipse/Maven issue after switch from log4j to logback. Take a look into your .classpath
file and search for the string "log4j"
.
In my case I had the following there:
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.7.1/slf4j-log4j12-1.7.1.jar"/>
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.17/log4j-1.2.17.jar" />
Removing those entries from the file (or you could regenerate it) fixed the issue.
Upvotes: 2
Reputation: 21
... org.codehaus.mojo cobertura-maven-plugin 2.7 test ch.qos.logback logback-classic tools com.sun ...
## I fixed with this
... org.codehaus.mojo cobertura-maven-plugin 2.7 test ch.qos.logback logback-classic tools com.sun ...
Upvotes: 2
Reputation: 9
Seems removing .m2 directory and :
mvn install -DskipTests -T 4
resolved this issue for me.
Upvotes: -6
Reputation: 2662
Just use only required dependency, not all :))). For me, for normal work of logging process you need this dependency exclude others from pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.8</version>
</dependency>
Upvotes: 3
Reputation: 3261
Sbt version:
Append exclude("org.slf4j", "slf4j-log4j12")
to the dependency that transitively includes slf4j-log4j12
. For example, when using Spark with Log4j 2.6:
libraryDependencies ++= Seq(
// One SLF4J implementation (log4j-slf4j-impl) is here:
"org.apache.logging.log4j" % "log4j-api" % "2.6.1",
"org.apache.logging.log4j" % "log4j-core" % "2.6.1",
"org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.6.1",
// The other implementation (slf4j-log4j12) would be transitively
// included by Spark. Prevent that with exclude().
"org.apache.spark" %% "spark-core" % "1.5.1" exclude("org.slf4j", "slf4j-log4j12")
)
Upvotes: 12
Reputation: 2897
Gradle version;
configurations.all {
exclude module: 'slf4j-log4j12'
}
Upvotes: 89
Reputation: 6186
Resolved by adding the following exclusion in the dependencies (of pom.xml) that caused conflict.
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
Upvotes: 173