Monday, November 20, 2017

Solving SLF4J: Class path contains multiple SLF4J bindings.

Problem:
While running unit tests, if you ever find these warnings, logged with INFO...

SLF4J: Class path contains multiple SLF4J bindings.
[INFO] SLF4J: Found binding in [jar:file:/C:/apache-maven-3.3.3/mvn.release.repo/org/slf4j/slf4j-log4j12/1.7.2/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: Found binding in [jar:file:/C:/apache-maven-3.3.3/mvn.release.repo/org/slf4j/slf4j-nop/1.7.2/slf4j-nop-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

The solution for fixing this is....
Use the maven surefire plugin to exclude the org.slf4j:slf4j-log4j12 usage during tests (slf4j-nop-1.7.2 is only used during tests)

pom.xml extract:

<build>
  <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
              <classpathDependencyExcludes>
                 <classpathDependencyExcludes> org.slf4j:slf4j-log4j12 </classpathDependencyExcludes>
              </classpathDependencyExcludes>
   </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>    <dependency>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-api</artifactId>     </dependency>     <dependency>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-nop</artifactId>         <version>1.7.2</version>         <scope>test</scope>     </dependency>   <dependency>

No comments:

Post a Comment