当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:多个SLF4J绑定

司徒高寒
2023-03-14

这可能是一个重复的问题,但我无法弄清楚绑定冲突在哪里。我有我的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依赖项:树输出

[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

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

你能发现错误吗?我将感谢你的帮助

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

共有3个答案

轩辕远
2023-03-14

当想要使用log4j进行日志记录,并排除默认的日志回溯实现时,如果还没有这个依赖项,就添加它,或者添加exclusion子句。

<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测井-如何。

慕容烨
2023-03-14

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

梁祯
2023-03-14

我也有同样的问题,只能看到来自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>
 类似资料:
  • 我正在尝试运行gradle从IntelliJ IDEA生成的战争。 tomcat实例运行时的输出:

  • 我得到以下错误。似乎有多个日志记录框架绑定到SLF4J。不知道该怎么解决。非常感谢任何帮助。

  • 我正在使用JBoss6,但在尝试运行我的应用程序时遇到一个多版本错误: 错误[STDERR]SLF4J:类路径包含多个SLF4J绑定。 错误[STDERR]slf4j:在[vfs:/c:/jboss-6.0.0.final/common/lib/slf4j-jboss-logmanager.jar/org/slf4j/impl/staticloggerbinder.class]中找到绑定 错误[S

  • 环境详细信息: IBM Worklight 6.2 但这不是解决办法。请提出解决方法。

  • 我在运行java代码时遇到以下运行时异常。有人能帮我解决绑定冲突吗。

  • 我在netbeans中有一个纯Java项目,我得到的警告是: 当然,我搜索了警告,但所有的答案都与用maven或pom.xml文件删除重复绑定有关。但是我没有pom.xml文件,也没有使用Maven。 那么如何排除绑定呢?