Sunday, May 23, 2021

Multiple SLF4J Bindings Error

 This tutorial explains how to fix Multiple SLF4J Bindings Error in maven project. Most of time you may have seen error during application development.

Multiple SLF4J Bindings Error


Error:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/path/to/jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/path/to/another/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 [org.slf4j.impl.Log4jLoggerFactory]


Solution

Case A: exclusion

Project X -> Project Y -> Project A

in Project X, when declaring Project Y as dependency, explicitly exclude Project A 

Add exclusions to dependency in pom

<dependencies>
    <dependency>
        <groupId> org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>0.8.1</version>

        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>

    </dependency>
</dependencies>


Case B: optional

Project X

  • Project Y -> Project A
  • Project Z -> Project A
  • Project A

in Project Y/Z' pom, set Project A as optional, then it will not be included in the classpath

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
    <optional>true</optional>
</dependency>


Case C: provided

  • Project X -> Project A
  • Project Y -> Project A
  • Project Z -> Project A

And X Y Z are all in classpath: set all but 1 to <scope>provided</scope>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
    <optional>true</optional>
    <scope>provided</scope>
</dependency>


Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.


No comments:

Post a Comment