gpt4 book ai didi

java - 覆盖运行时 SLF4J 绑定(bind)以进行测试

转载 作者:行者123 更新时间:2023-11-29 04:25:23 25 4
gpt4 key购买 nike

我正在写一个包。我需要 slf4j-log4j12 以及运行时的特定附加程序。但是对于测试,我只需要一个 slf4j-simple 绑定(bind)。所以,我的 pom.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<groupId>com.bharani</groupId>
<artifactId>LoggingDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- Direct dependencies -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>

当我运行我的程序时,我得到了预期的 log4j 日志。但是当我运行测试时,我得到:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/bharani/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/bharani/.m2/repository/org/slf4j/slf4j-simple/1.7.25/slf4j-simple-1.7.25.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 [org.slf4j.impl.Log4jLoggerFactory]

...而且我没有在 slf4j-simple 绑定(bind)中按预期在控制台中看到日志。

我该如何解决这个问题?如何让 runtimeslf4j-log4j12 的依赖远离测试?

谢谢。

最佳答案

问题是 runtime 范围的依赖项包含在测试类路径中,因此 SLF4J 最终有两个绑定(bind)并且必须选择其中一个(它选择的绑定(bind)可能是随机的,碰巧它在你的情况下选择了 log4j)。

一个简单的解决方案是从 Surefire 插件的类路径中删除 log4j 绑定(bind):

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExcludes>org.slf4j:slf4j-log4j12</classpathDependencyExcludes>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>

更多信息请参见 http://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html .

关于java - 覆盖运行时 SLF4J 绑定(bind)以进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com