【问题标题】:SLF4J: Class path contains multiple SLF4J bindings with mavenSLF4J:类路径包含多个与 maven 的 SLF4J 绑定
【发布时间】:2014-12-23 15:21:25
【问题描述】:

我使用 - SLF4J 在我的应用程序中创建了自定义日志记录,遵循以下链接,

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>

谁能帮我解决这个问题?

【问题讨论】:

  • 尝试添加compile到logback-classic依赖

标签: java spring maven slf4j logback


【解决方案1】:

根据错误消息 (http://www.slf4j.org/codes.html#multiple_bindings) 中给出的链接,SLF4J 旨在“一个仅一个 底层日志框架在一次”。

您的类路径中目前有两个:

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

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

你基本上有两种选择:

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

【讨论】:

  • 我可能遗漏了一些东西,但对我来说,您的两个选项非常明确,要么使用 logback,要么使用您的自定义日志记录。如果您使用自定义日志记录,那么您将完全删除 logback,包括其配置。即也删除 logback.xml 文件。唯一的另一种可能性是在不通过 SLF4J 的情况下以某种方式使用 logback,但据我所知,不使用 SLF4J 就无法使用 logback,因此这是行不通的。
猜你喜欢
  • 2014-05-18
  • 2020-07-07
  • 2012-12-11
  • 2016-03-12
  • 2015-08-10
  • 2021-05-19
  • 1970-01-01
  • 2018-12-12
  • 2012-09-11
相关资源
最近更新 更多