Gradle将SLF4J simple用于测试,将Log4j12用于运行时

Gradle将SLF4J simple用于测试,将Log4j12用于运行时,gradle,slf4j,Gradle,Slf4j,我在测试期间使用了slf4j,我希望通过使用slf4j simple在控制台上看到日志输出。然后运行时将使用log4j配置 我可以在maven中通过这样声明依赖项来做到这一点 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version

我在测试期间使用了slf4j,我希望通过使用slf4j simple在控制台上看到日志输出。然后运行时将使用log4j配置

我可以在maven中通过这样声明依赖项来做到这一点

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
    <scope>compile</scope>
</dependency>
<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>${slf4j.version}</version>
  <scope>test</scope>
</dependency>    
<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>${slf4j.version}</version>
  <scope>runtime</scope>
</dependency>
但是在构建过程中,我可以看到它仍然在使用Log4j12实现。我该如何解决这个问题

SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [org.slf4j.log4j12.Log4j12ServiceProvider@4ad9cc78]
SLF4J: Found provider [org.slf4j.simple.SimpleServiceProvider@6e869e5e]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual provider is of type [org.slf4j.log4j12.Log4j12ServiceProvider@4ad9cc78]
你可以试试

gradle dependencies --configuration testRuntime

要查看它的来源,请将其添加到您的一个测试中

String path = "org/slf4j/log4j12/Log4j12ServiceProvider.class";
Enumeration<URL> urls = getClass().getClassLoader().getResources(path);
while (urls.hasMoreElements()) {
    System.out.println(String.format("found %s at %s", path, urls.nextElement()));
} 
String path=“org/slf4j/log4j12/Log4j12ServiceProvider.class”;
枚举URL=getClass().getClassLoader().getResources(路径);
while(url.hasMoreElements()){
System.out.println(String.format(“在%s处找到%s”,路径,url.nextElement());
} 

我感觉buildscript类路径不知何故泄漏到junit类路径中。尝试将此添加到build.gradle中

task showInConfigurations {
   doLast {
      String path = "org/slf4j/log4j12/Log4j12ServiceProvider.class"
      def configs = [buildscript.configurations.classpath, configurations.testRuntime]
      configs.each { config ->
         config.files.each { file ->
            if (file.name.endsWith('.jar')) {
               Set<File> matches = zipTree(file).matching { include path}.files
               if (matches) println "Found $path in $config.name in $file" 
            } 
         } 
       } 
    } 
} 
任务显示配置{
多拉斯特{
String path=“org/slf4j/log4j12/Log4j12ServiceProvider.class”
def configs=[buildscript.configurations.classpath,configurations.testRuntime]
configs.each{config->
config.files.each{file->
if(file.name.endsWith('.jar')){
Set matches=zipTree(文件)。匹配{include path}.files
如果(匹配)println“在$file中的$config.name中找到$path”
} 
} 
} 
} 
} 

如果它不在
testRuntime
配置中,我想您可能已经发现了一个gradle bug

gradle dependencyInsight--配置testRuntime--dependency slf4j-log4j12
给出“在配置中未找到与给定输入匹配的依赖项”:无法生成testRuntime“Yup”dependency insight报告,因为未指定要显示的依赖项。',尝试
gradle依赖项--configurationtestruntime
。我总是把这两个任务搞混HMMM,最新的一个任务将slf4j-log4j12显示为根依赖项,就像我显式声明它为testRuntim一样?!?!如果我将log4j依赖项更改为runtimeOnly,它将不再显示在依赖项输出中,但似乎仍然显示在类路径中jar:file:/C:/Users/Paul/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.8.0-beta2/ee99e18c77147a4d7619a06683e70e741b95641b/slf4j-log4j12-1.8.0-beta2.jar/org/slf4j/log4j12/Log4j12ServiceProvider.class`这看起来像是
1.8.0-beta2版本
与您的
slf4j\u版本
匹配吗?或者它是不同的?是的,与版本匹配。没有输出,这是未测试的。也许可以尝试将“org.slf4j:slf4j-log4j12:$slf4j_VERSION”添加到测试运行时依赖项中,然后运行
gradle showInConfigurations
,以便在gradle命令行上进行健全的测试?还是只在IDE中?我通过命令行来完成
task showInConfigurations {
   doLast {
      String path = "org/slf4j/log4j12/Log4j12ServiceProvider.class"
      def configs = [buildscript.configurations.classpath, configurations.testRuntime]
      configs.each { config ->
         config.files.each { file ->
            if (file.name.endsWith('.jar')) {
               Set<File> matches = zipTree(file).matching { include path}.files
               if (matches) println "Found $path in $config.name in $file" 
            } 
         } 
       } 
    } 
}