Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

SLF4J: Class path contains multiple SLF4J bindings

farenda 2016-08-04 0

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 https://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!

References:

  • SLF4J error with too few bindings
Share with the World!
Categories Java Tags java, logging, slf4j
Previous: SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder
Next: Java CyclicBarrier – game simulation

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored