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

Java 如何将多个SLF4J绑定排除到LOG4J,java,logging,gradle,slf4j,log4j2,Java,Logging,Gradle,Slf4j,Log4j2,我发现了错误 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: 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)

在从属项目的另一个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文档并没有真正让它变得更清晰。

排除包含您不想使用的slf4j.jar的依赖项。因为格雷德尔可能会帮上忙

更新:

不幸的是,我不熟悉gradle,但从链接文档判断,像

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

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

解决方案是在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"
}

为了完整地介绍commons日志。

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

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

是的,这正是我想要做的。我只是无法从Gradle文档中弄清楚具体如何做到这一点。不应该路由到slf4j,而应该使用log4j-1.2-api jar将log4j 1.x调用路由到log4j2。@rgoers:仅仅添加log4j-1.2-api jar并不能解决多依赖性问题。
configurations.all {
   exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}