Java Programming Tutorials

Java programming tutorials with many code examples!

SLF4J: Class path contains multiple SLF4J bindings

SLF4J: Class path contains multiple SLF4J bindings error occurs in many projects, especially those having multiple dependencies. Here we the reason and how to deal with it!

Erroneous project configuration

Here is sample Maven project configuration that brings more than one SLF4J binding into the classpath:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
    <scope>runtime</scope>
</dependency>

Sample Java code that writes to logs:

This simple code will allow us to see the error:

package com.farenda.loggers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingWithSlf4j {

    private static final Logger log
            = LoggerFactory.getLogger(LoggingWithSlf4j.class);

    public static void main(String[] args) {
        log.info("Hello Slf4j!");
    }
}

The above code produces the following output:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/przemek/.m2/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/przemek/.m2/repository/org/slf4j/slf4j-simple/1.7.21/slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

Binding SLF4J to only one logging framework

Remember that SLF4J is only a facade to logging frameworks – slf4j-api. It finds wrappers for all available logging frameworks on the runtime classpath and tries to load them. The error tells us that it found more than one logging framework and don’t know which one is correct.

To fix the error we have choose only one logging framework that should be used by SLF4J at runtime. In case of simple duplication in Maven POM you just need to remove redundant logging framework bindings – slf4j-log4j12 or slf4j-simple in our case.

Usually the culprit is not that obvious and is hidden in transitive dependencies. To find all SLF4J bindings you can list effective POM.

Leaving only one of the bindings makes the SLF4J to work as expected:

[main] INFO com.farenda.loggers.LoggingWithSlf4j - Hello Slf4j!
Share with the World!