user1660325
user1660325

Reputation: 757

SLF4J: Class path contains multiple SLF4J bindings with maven

I have created cutom logging in my application using - SLF4J, following the BELOW Link,

http://javaeenotes.blogspot.com/2011/12/custom-slf4j-logger-adapter.html

Its working fine with a sample HelloWorldProgram this way,

public class HelloWorld {
    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);
        logger.info("Hello World");
    }
}

But if i integrate the same with my maven project , where i am using spring to create log file,

I am getting the below error.Tried many different ways to solve the issue, but no luck.

11:08:59,486 ERROR [STDERR] SLF4J: Class path contains multiple SLF4J bindings.
11:08:59,486 ERROR [STDERR] SLF4J: Found binding in [/WEB-INF/classes/org/slf4j/impl/StaticLoggerBinder.class]
11:08:59,486 ERROR [STDERR] SLF4J: Found binding in [/WEB-INF/lib/logback-classic-1.0.13.jar/org/slf4j/impl/StaticLoggerBinder.class]
11:08:59,486 ERROR [STDERR] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
11:08:59,496 ERROR [STDERR] SLF4J: Actual binding is of type [org.slf4j.impl.MyLoggerFactory]

Pom.xml

<dependencies>

    <!-- spring core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
        <scope>provided</scope>
    </dependency>

    <!-- logging dependencies -->
    <!-- <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <version>${slf4j.version}</version>
    </dependency> -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.9</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
        <exclusions>
            <exclusion>
                <!-- Defined below -->
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.13</version>
        <exclusions>
            <exclusion>
                <!-- Defined below -->
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <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>
            <exclusion> 
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
            </exclusion>
        </exclusions> 
    </dependency>    

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0</version>
        <exclusions>
            <exclusion>
                <groupId>commons-configuration</groupId>
                <artifactId>commons-configuration</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
            </exclusion>
            <exclusion>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xom</groupId>
                <artifactId>xom</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.beanshell</groupId>
                <artifactId>bsh-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.owasp.antisamy</groupId>
                <artifactId>antisamy</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>                        
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <classpathDependencyExcludes>
                <classpathDependencyExcludes>ch.qos.logback:logback-classic</classpathDependencyExcludes>
            </classpathDependencyExcludes>
        </configuration>
        </plugin> 
    </plugins>
</build>

Logback.xml

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
</contextListener>


<appender name="appLogFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            //log file creation goes here
        </rollingPolicy>        
</appender>

Can anyone help me in this issue?

Upvotes: 0

Views: 6788

Answers (1)

DB5
DB5

Reputation: 13988

As per the link given in the error message (http://www.slf4j.org/codes.html#multiple_bindings), SLF4J is designed to "bind with one and only one underlying logging framework at a time".

You currently have two on your classpath:

  • your own custom binder (/WEB-INF/classes/org/slf4j/impl/StaticLoggerBinder.class)
  • and the one from logback-classic

When SLF4J finds multiple bindings it simply randomly picks one and uses that. In your case it picked your custom binding (based on the last line of the error message "Actual binding is of type [org.slf4j.impl.MyLoggerFactory]"), but it is not guaranteed to do this every time.

You basically have two options:

  • If you want to use your own custom binder then you should remove the logback dependencies from your POM.
  • Alternatively you can obviously remove your own custom binder (but I am guessing that is not what you want).

Upvotes: 1

Related Questions