【问题标题】:Spring Boot: multiple SLF4J bindingsSpring Boot:多个 SLF4J 绑定
【发布时间】:2016-01-09 08:01:58
【问题描述】:

这可能是一个重复的问题,但我无法弄清楚绑定碰撞在哪里。我有我的Spring Boot 1.2.6.RELEASE 服务,运行它时出现此错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.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]

这是mvn dependency:tree 输出

[INFO] com.myenterprise:product-manager:jar:0.0.1-SNAPSHOT
[INFO] +- com.myenterprise.product:myproject-api:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.myenterprise.product:myproject-core:jar:0.0.1-SNAPSHOT:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.6.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.6.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.26:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.26:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.26:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.26:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.6:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.6:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.6:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] |  +- org.springframework:spring-core:jar:4.1.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:4.1.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:4.1.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.1.7.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:4.1.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.6.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.6.RELEASE:compile
[INFO] |  |  +- org.aspectj:aspectjrt:jar:1.8.6:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.6.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-jdbc:jar:4.1.7.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.0.26:compile
[INFO] |  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.0.26:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:4.1.7.RELEASE:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework:spring-orm:jar:4.1.7.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.7.3.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.9.3.RELEASE:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.1.7.RELEASE:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.36:compile
[INFO] +- org.flywaydb:flyway-core:jar:3.1:compile
[INFO] +- org.springframework.security.oauth:spring-security-oauth2:jar:2.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.1.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.1.7.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-core:jar:3.2.8.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework.security:spring-security-config:jar:3.2.8.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-web:jar:3.2.8.RELEASE:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |     \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.2.6.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.2.6.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.6.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.14:compile
[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.6.RELEASE:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.12:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.2.6.RELEASE:compile
[INFO]    +- junit:junit:jar:4.12:compile
[INFO]    +- org.mockito:mockito-core:jar:1.10.19:compile
[INFO]    |  \- org.objenesis:objenesis:jar:2.1:runtime
[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO]    +- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO]    \- org.springframework:spring-test:jar:4.1.7.RELEASE:compile

所以,显然问题是logback 仍在类路径中,但我在输出中找不到它,所以我不知道问题出在哪里。

你能发现错误吗?非常感谢您的帮助


编辑

这只发生在我将服务器作为 Spring Boot 应用程序运行时。如果我将它作为 java 应用程序运行,错误就消失了......有什么想法吗?

【问题讨论】:

  • 你是如何将它作为 Spring Boot 应用程序运行的?我不明白你的意思。
  • @M.Deinum 我指的是 STS 提供的启动器
  • 可能是(不小心)添加了一些依赖项,不确定有一段时间没有使用 STS :)。

标签: java maven spring-boot slf4j


【解决方案1】:

我遇到了完全相同的问题,只能看到来自 spring boot 的依赖。它还引入了 log4j-over-slf4j,这与我自己对 slf4j-log4j12 的要求相冲突。通过添加以下排除项解决。这比不包括 Spring Boot 日志记录更具体。

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

【讨论】:

  • 如果我这样做,我会得到一个新的“错误”:log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)。 log4j:WARN 请正确初始化 log4j 系统。 log4j:WARN 请参阅logging.apache.org/log4j/1.2/faq.html#noconfig 了解更多信息。如果我从排除列表中省略 log4j-over-slf4j,我会得到: SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError。 SLF4J:请参阅slf4j.org/codes.html#log4jDelegationLoop 了解更多详情。
  • 我得到相同的行为,如果我如上所述排除,则 log4j 未初始化。如果我不排除,我会收到类路径错误。
【解决方案2】:

由于所有早期的答案都没有成功让 log4j2 工作(至少对我来说不是),我在https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging 找到了解决方案。基本上,从 org.springframework.boot:spring-boot-starter 中排除 org.springframework.boot:spring-boot-starter-logging 并在 pom.xml 中添加 org.springframework.boot:spring-boot-starter-log4j2 作为依赖项。

【讨论】:

    【解决方案3】:

    删除对spring-boot-starter-log4j 的依赖,它引入了slf4j-log4j12,如果你有一些依赖于log4j 的代码,我认为你应该使用log4j-over-slf4j。请参阅here 了解区别。

    【讨论】:

    • 如果我将它作为 Spring Boot 应用程序运行,这解决了问题,但如果我将它作为 Java 应用程序运行它会失败(与开始时发生的情况相反)。有任何想法吗?谢谢
    • 两种启动方式有什么区别?
    • 正如doc 上所记录的那样,显然当将应用程序作为 Spring Boot 应用程序运行时,它会添加一些东西
    • 我用过spring-boot,但是这里没有运行示例。因此,我不确定如何提供进一步的帮助。我建议在 spring boot 中找到设置记录器的代码,并检查它是否在两种启动模式下都被调用。
    【解决方案4】:

    当想要使用 log4j 进行日志记录,并排除默认的 logback 实现时,如果还没有,请添加此依赖项,或者添加排除子句。

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

    请参考Spring Logging - How To

    【讨论】:

    • 这是这个问题的答案。
    【解决方案5】:

    你在你的类路径上使用 logback.xml 并且你的 pom 上有 spring-boot-starter-log4j:jar 这就是为什么要使用 StaticLoggerBinder 的冲突。

    SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/home/jscherman/.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    

    请从您的 pom 中删除任一日志库,例如 spring-boot-starter-log4j 以消除冲突。

    【讨论】:

      【解决方案6】:

      这个错误是由于类路径中有多个 SLF4J 实现引起的。 SLF4J 是对各种日志框架(如 log4j 或 logback)的抽象,在编译阶段通过静态方法检测实现。

      从你的dependency:tree,我只找到log4j,没有logback相关的jar

      [INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.6.RELEASE:compile
      [INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
      [INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile
      [INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.12:compile
      [INFO] |  \- log4j:log4j:jar:1.2.17:compile
      

      你应该先尝试运行maven-update project,然后再检查一遍。

      【讨论】:

      • 我做到了,还是一样。还是谢谢
      • 我有一个解决方法,你可以从pom.xml中一一去掉依赖来查看logback相关的jar文件涉及到哪个
      【解决方案7】:

      本页上述所有答案都是正确的,指向同一个方向,但过程相当具有挑战性和繁琐。

      我强烈建议使用或获取标准插件的帮助,例如Maven helper plugin

      【讨论】:

        【解决方案8】:

        Maven Helper 插件非常有助于快速可视化依赖关系图并识别冲突并通过简单的点击内联解决它们。

        【讨论】:

        • 您的回答并不是问题的真正答案。我认为这将是一个很好的评论,因为它可能会帮助 jscherman 走向正确的方向。
        • +1 非常不错的插件。多年来一直使用 IntelliJ,但并不知道这一点。虽然它没有在“冲突”中找到我的冲突导入,但我设法通过依赖列表找到了冲突导入的根源。
        【解决方案9】:

        如果其他人将来遇到同样的问题,请发布此问题。

        在大型项目中,会有多个依赖项,将排除项放在其中的一两个中可能并不总是有效。在我的情况下,我也无法在我试图运行的项目的依赖树中找到logback-classic。对我来说,原因是

        • 构建路径中包含其他内部项目。这些项目使用的是logback-classic

        解决方案:

        • 第一步是了解项目中实际需要什么,即logback-classicslf4j-api
        • 首先,您可以开始将 slf4j-api 排除在依赖项中。
        • 如果您开始遇到任何编译错误,则需要 slf4-api,您只需对 logback-classic 重复相同的过程,直到它完全从依赖关系树和其他内部项目中消失。
        • 如果您没有收到任何编译错误,则必须继续从依赖项中排除 slf4j-api
        • 如果内部项目也包含在构建路径中,则可能还必须为这些项目重复该过程

        【讨论】:

          猜你喜欢
          • 2023-02-22
          • 2015-04-24
          • 1970-01-01
          • 2020-07-07
          • 1970-01-01
          • 2018-07-10
          • 2012-09-28
          • 2015-05-28
          • 1970-01-01
          相关资源
          最近更新 更多