Java Spring Boot:多个 SLF4J 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33071002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:16:19  来源:igfitidea点击:

Spring Boot: multiple SLF4J bindings

javamavenspring-bootslf4j

提问by jscherman

This is probably a repeated question, but i can't figure it out where is the binding collision. I have my Spring Boot 1.2.6.RELEASEservice and i'm getting this error when i run it:

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

This is the mvn dependency:treeoutput

这是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.Hymanson.core:Hymanson-databind:jar:2.4.6:compile
[INFO] |  |  +- com.fasterxml.Hymanson.core:Hymanson-annotations:jar:2.4.6:compile
[INFO] |  |  \- com.fasterxml.Hymanson.core:Hymanson-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.Hymanson:Hymanson-mapper-asl:jar:1.9.13:compile
[INFO] |     \- org.codehaus.Hymanson:Hymanson-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

So, apparently the problem is that logbackis still in classpath, but i couldn't find it in the output so i don't know where is the problem.

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

Can you spot the error? I would appreciate your help

你能发现错误吗?我很感激你的帮助



EDIT

编辑

This only happens when i run the server as Spring Boot Application. If i run it as java application, the error is gone... any ideas?

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

回答by Avis

You using logback.xml on your classpath & have spring-boot-starter-log4j:jar on your pom thats why the conflict for which StaticLoggerBinderto use.

您在类路径上使用 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]

Pls remove either one of the logging library, say spring-boot-starter-log4j from your pom to remove the conflict.

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

回答by David Roussel

Remove the dependency on spring-boot-starter-log4jwhich is bringing in slf4j-log4j12, I think you should use use log4j-over-slf4jif you have some code that depends on log4j. See herefor a distinction.

删除对spring-boot-starter-log4jwhich islining 的依赖,如果您有一些依赖 log4j 的代码slf4j-log4j12,我认为您应该使用 use log4j-over-slf4j。请参见此处的区别。

回答by user1461926

This error is caused by there are more than one SLF4J implementation in class path . SLF4J is an abstraction for various logging frameworks(such as log4j or logback) ,which detects the implementation during compile phase by static method .

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

From your dependency:tree , I only find log4j ,there is no logback related jar

从您的依赖项:树,我只找到 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

You should try to run maven - update project firstly , and check it again .

您应该先尝试运行 maven-update 项目,然后再检查一次。

回答by Stephen

I had the exact same problem and could only see the dependency coming from spring boot. It also brought in log4j-over-slf4j, which clashed with my own requirement of slf4j-log4j12. Solved by adding the exclusions below. This is more specific that excluding the spring boot logging.

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

<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>

回答by TechFree

When want to use log4j for logging, and exclude default logback implementation, add this dependency if not already there, or add the exclusion clause.

当想要使用 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>

Refer Spring Logging - How To.

请参阅Spring Logging - How To

回答by SaschaH

Since all earlier answers haven't been successful to get log4j2 working (at least not for me), I found the solution at https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging. Basically, exclude org.springframework.boot:spring-boot-starter-loggingfrom org.springframework.boot:spring-boot-starterand add org.springframework.boot:spring-boot-starter-log4j2as dependency in your pom.xml.

由于所有早期的答案都没有成功让 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-logging来自org.springframework.boot:spring-boot-starter添加org.springframework.boot:spring-boot-starter-log4j2在你的依赖pom.xml

回答by Himadri Mandal

All the above answers on this page are correct and point to the same direction, but the procedure is quite challenging and cumbersome.

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

I highly advise to use or take help of a standard plugin e.g. Maven helper plugin

我强烈建议使用或使用标准插件,例如Maven 助手插件