Java 如何排除多个 SLF4J 绑定到 LOG4J

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18952479/
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-12 12:51:39  来源:igfitidea点击:

How to exclude multiple SLF4J bindings to LOG4J

javalogginggradleslf4jlog4j2

提问by giorgio

I am getting the error

我收到错误

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/george/.gradle/caches/artifacts-26/filestore/org.apache.logging.log4j/log4j-slf4j-impl/2.0-beta8/jar/15984318e95b9b0394e979e413a4a14f322401c1/log4j-slf4j-impl-2.0-beta8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/george/.gradle/caches/artifacts-26/filestore/org.slf4j/slf4j-log4j12/1.5.0/jar/aad1074d37a63f19fafedd272dc7830f0f41a977/slf4j-log4j12-1.5.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

In my build.gradle file I have the following line to include the jar log4j-slf4j-impl-2.0-beta8.jar (which I want to bind to LOG4J2)

在我的 build.gradle 文件中,我有以下行来包含 jar log4j-slf4j-impl-2.0-beta8.jar(我想绑定到 LOG4J2)

 compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.0-beta8' 

In another build.gradle file in a dependent project I have multiple lines similar to the following:

在依赖项目的另一个 build.gradle 文件中,我有多行类似于以下内容:

 compile 'dcm4che:dcm4che-core:2.0.23'

Now dcm4che includes a dependency on log4j version 1 (slf4j-log4j12) and this is therefore being included in the overall project.

现在 dcm4che 包含对 log4j 版本 1 (slf4j-log4j12) 的依赖,因此这被包含在整个项目中。

Here is a snippet from the Gradle dependency tree:

这是 Gradle 依赖关系树中的一个片段:

|    +--- dcm4che:dcm4che-core:2.0.23
|    |    \--- org.slf4j:slf4j-log4j12:1.5.0
|    |         +--- org.slf4j:slf4j-api:1.5.0 -> 1.7.5
|    |         \--- log4j:log4j:1.2.13 -> 1.2.14

I have read the link suggested in the warningbut I cannnot figure out how to make my app bind to log4j2 using the jar that I want. The Gradle documentation on dependency management has not really made it any clearer.

我已阅读警告中建议的链接 但我无法弄清楚如何使用我想要的 jar 将我的应用程序绑定到 log4j2。关于依赖管理的 Gradle 文档并没有真正使它更清晰。

采纳答案by giorgio

The solution is to add the following in the build.gradle.

解决方法是在build.gradle中添加以下内容。

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'log4j') {
            details.useTarget "org.slf4j:log4j-over-slf4j:1.7.5"
        }
}

The result is that anything that normally requires log4j will use log4j-over-slf4j instead.

结果是任何通常需要 log4j 的东西都将使用 log4j-over-slf4j。

I also added:

我还补充道:

if (details.requested.name == 'commons-logging') {
    details.useTarget "org.slf4j:jcl-over-slf4j:1.7.5"
}

for completeness to cover commons logging.

为了完整性以涵盖公共日志记录。

回答by Pyranja

Exclude the dependency that contain the slf4j .jar you do notwant to use. For gradle thismay help in doing so.

排除包含SLF4J的.jar你的依赖性希望使用。对于gradle,可能有助于这样做。

update:

更新:

Unfortunately I am not familiar with gradle, but judging from the linked documentation sth. like

不幸的是,我不熟悉 gradle,但从链接的文档来看。喜欢

compile('dcm4che:dcm4che-core:2.0.23') {
   exclude group: 'org.slf4j'
}

may work? Note that the documentation mentions a sort of 'configuration-global' exclusion list, which may be a better way to do this, but I failed to find more information on that.

可以工作吗?请注意,文档中提到了一种“配置全局”排除列表,这可能是一种更好的方法,但我未能找到更多相关信息。

回答by Quy Tang

Put this code in your build.gradlefile

将此代码放入您的build.gradle文件中

configurations.all {
   exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}