【问题标题】:SLF4J collision in Spring BootSpring Boot 中的 SLF4J 冲突
【发布时间】:2020-02-04 12:11:37
【问题描述】:

我的 pom 文件:

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.1</version>
        </dependency>
    </dependencies>
</project>

当我开始一个应用程序时,我遇到了冲突:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/krasnov/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/krasnov/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

如何解决?我想使用log4j-slf4j-impl 进行日志记录。

如果我使用:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

然后在日志文件中有我的日志和更多我不需要的系统日志

【问题讨论】:

  • 尝试从 Spring Boot 中单独排除日志库,而不是整个 starter-logging。我认为 Spring 使用 logback-classic 进行日志记录,所以排除它。

标签: java spring spring-boot log4j


【解决方案1】:

您的项目中实现了两个记录器:

  • logback-classic
  • log4j-slf4j

    您需要排除任何一个有冲突的依赖项,因为您已经决定不想排除log4j-slf4j,因此您只能排除logback-classic。尝试如下所示。

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency>

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2023-04-04
    • 2018-02-21
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多