SLF4J: Class path contains multiple SLF4J bindings

422,485

Solution 1

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> 

Solution 2

Gradle version;

configurations.all {
    exclude module: 'slf4j-log4j12'
}

Solution 3

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:

dependency tree conflict

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)

Solution 4

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")
)

Solution 5

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.

  1. Resolution

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'
}
Share:
422,485

Related videos on Youtube

user1493140
Author by

user1493140

Updated on July 08, 2022

Comments

  • user1493140
    user1493140 almost 2 years

    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.
    
    • user1493140
      user1493140 over 11 years
      Resolved Using <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> in the dependecies (of pom.xml) that caused conflict helped resolve the problem
    • michael
      michael about 11 years
    • Peter Keller
      Peter Keller about 10 years
      Did you already check slf4j.org/codes.html#multiple_bindings as stated in the warning?
    • Roberto
      Roberto about 10 years
      Maybe It could be better to add an answer (auto-answer) to this question and mark it as "Accepted", So the question will appear as "Solved" in the SO searchs
    • user1493140
      user1493140 about 10 years
      Roberto, Thanks for feedback. I copied the solution from the comment and posted it as answer.
    • Amit Sinha
      Amit Sinha almost 5 years
      I am getting the same error but in Pom.xml file we don't have slf4j dependency.
    • granadaCoder
      granadaCoder almost 5 years
      For future readers, here is a good article for log4j2 (<<emphasis on the "2" here) logicbig.com/tutorials/spring-framework/spring-boot/log4j2.h‌​tml the key phrase in the article is : "spring-boot-starter-log4j2 is a starter for using Log4j2. It is an alternative to spring-boot-starter-logging."
    • Master Mind
      Master Mind over 2 years
      Add exclude dependency with the dependency which has been placed at first.
  • PUG
    PUG over 9 years
    which dependancy caused conflict in this case, I have dependancy tree there are 3 mentions of slf4j
  • Will
    Will about 9 years
    Importing Models from the main application into automation framework.. this resolved my problem with gradle. ty.
  • Balaji Boggaram Ramanarayan
    Balaji Boggaram Ramanarayan almost 9 years
    Is there any ant version of doing this ?
  • cyber-monk
    cyber-monk over 8 years
    to find out how log4j is getting on your path run mvn dependency:tree and comb through, then add the snippet above to that dependency in your pom.xml
  • Matthew Mark Miller
    Matthew Mark Miller about 8 years
    no: ant is not a dependency aware tool. Strongly consider porting your build to gradle.
  • ioleo
    ioleo almost 7 years
    strongly recomend porting your build to sbt :)
  • Ashok kumar Ganesan
    Ashok kumar Ganesan over 6 years
    @user1493140 Still it is not working for me. <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
  • Petrus Theron
    Petrus Theron about 6 years
    What does Sbt stand for?
  • PlunkettBoy
    PlunkettBoy about 6 years
    Excellent. Saved me from a few hours of dependency hell!
  • Benny
    Benny almost 6 years
    Simple Build Tool*
  • Jan Lochman
    Jan Lochman over 5 years
    Thank you for introducing mvn dependency:tree. It's so much helpful...
  • ihebiheb
    ihebiheb about 5 years
    for me another jar war the cause. I had to exclude the artifacts with id slf4j-nop and slf4j-jdk14. The dependency that caused the conflict for me was clover-maven-plugin
  • Lei Yang
    Lei Yang over 4 years
    not clear: do you mean delete/comment above xml section, or add?
  • Lei Yang
    Lei Yang over 4 years
    is the version (slf4j-log4j12) applicable for all? or should we find out the version from mvn dependency:tree?
  • Jhon Hernández
    Jhon Hernández almost 3 years
    After number 1, the other options doesn´t appera anymore...thanks more things to fix
  • Partha Paul
    Partha Paul over 2 years
    How to do in Maven?
  • Kerem
    Kerem over 2 years
    @ParthaPaul the previous answer is the one for that. stackoverflow.com/a/22638585/1505341