SLF4J:类路径包含多个与maven的SLF4J绑定

时间:2014-12-23 06:10:57

标签: java spring maven slf4j logback

我使用 - SLF4J在我的应用程序中创建了cutom日志记录,遵循BELOW Link,

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

以这种方式使用HelloWorldProgram样本正常工作,

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

但如果我将其与我的maven项目集成,我使用spring创建日志文件,

我收到以下错误。有许多不同的方法可以解决问题,但没有运气。

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>

有谁可以帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

根据错误消息(http://www.slf4j.org/codes.html#multiple_bindings)中给出的链接,SLF4J旨在&#34; 一个一个一次基础日志框架&#34;。

您目前在类路径上有两个:

  • 您自己的自定义活页夹(/WEB-INF/classes/org/slf4j/impl/StaticLoggerBinder.class)
  • 和logback-classic
  • 中的那个

当SLF4J找到多个绑定时,它只是随机选择一个并使用它。在您的情况下,它选择了您的自定义绑定(基于错误消息的最后一行&#34; Actual binding is of type [org.slf4j.impl.MyLoggerFactory]&#34;),但不保证每次都这样做。

您基本上有两个选择:

  • 如果您想使用自己的自定义绑定器,那么您应该从POM中删除logback依赖项。
  • 或者您可以显然删除自己的自定义活页夹(但我猜这不是您想要的)。