如何排除与 LOG4J 的多个 SLF4J 绑定



我收到错误

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.

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

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

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

 compile 'dcm4che:dcm4che-core:2.0.23'

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

以下是 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

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

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

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

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

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

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

我还补充说:

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

为了完整起见,以涵盖共享资源日志记录。

排除包含您不想使用的 slf4j .jar依赖项。对于 gradle,这可能有助于这样做。

更新:

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

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

可能有效吗?请注意,文档提到了一种"配置全局"排除列表,这可能是执行此操作的更好方法,但我未能找到有关此的更多信息。

最新更新