Question

i'm trying to use logback in my project but there is some problem . when i run my code it gives me this:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/mostafa/.m2/repository/org/slf4j/slf4j-log4j12/1.7.2/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/mostafa/.m2/repository/ch/qos/logback/logback-classic/1.1.1/logback-classic-1.1.1.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]
log4j:WARN No appenders could be found for logger (com.project.Test).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

my logback.xml is under resources folder:

$ tree sample

|-- pom.xml
`-- src
    `-- main
        |-- java
        |  
        |-- resources
        |   `-- logback.xml
     

and my project structure is :

enter image description here

Was it helpful?

Solution

If you carefully read the error messge provided, you will notice that your problem is multiple bindings of sfl4j on your build path. You included sfl4j-log4j and logback-classic artifacts. Since logback-classic already contains native implementation of sfl4j and also automatically adds sfl4j jar to your project you end up having mutiple sf4j's in your build path. Simply make up your mind and choose only one logging framework and remove another one from your dependencies.

So if you choose logback-classic your Maven dependencies will look like this:

<dependency> 
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.1.1</version>
</dependency>

If you choose log4j then make it look like this

<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.2</version>
</dependency>

But don't include both!

From sfl4j manual:

If you wish to use logback-classic as the underlying logging framework, all you need to do is to declare "ch.qos.logback:logback-classic" as a dependency in your pom.xml file as shown below. In addition to logback-classic-1.0.13.jar, this will pull slf4j-api-1.7.7.jar as well as logback-core-1.0.13.jar into your project. Note that explicitly declaring a dependency on logback-core-1.0.13 or slf4j-api-1.7.7.jar is not wrong and may be necessary to impose the correct version of said artifacts by virtue of Maven's "nearest definition" dependency mediation rule

OTHER TIPS

Are you using cassandra or anythings else that may add the log4j artifact to your library?

If you add cassandra-all artifact to dependency and since the cassandra used log4j as the the SLF4J backend for self logging , it import slf4j-log4j12.jar

You can edit pom.xml like this:

    <dependency>
        <groupId>org.apache.cassandra</groupId>
        <artifactId>cassandra-all</artifactId>
        <version>2.0.5</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

also read following link:

http://www.slf4j.org/codes.html#multiple_bindings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top