我正在使用 SpringRunner
类来指导我的 IT 测试:
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = MyTestConfiguration.class)
@RunWith(SpringRunner.class)
class SomeTestIT {
// tests here
}
@Configuration
public class MyTestConfiguration {
}
我有以下 log4j2 的 Maven 依赖项:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
我的 log4j2.xml
如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration strict="true" monitorInterval="30" status="error">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%5p | %d | %F | %L | %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
我已将此 log4j2.xml
放入 src/test/resources
中。
现在,当测试运行时,我收到以下警告:
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
出于某种原因,我找不到,我的 log4j2 配置未用于 SpringRunner
的日志记录。我该如何调试并希望解决这个问题?
更新
我认为它不相关,但以防万一:我还有以下依赖项:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
最佳答案
这是旧的 log4j 绑定(bind):
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
此处 slf4j-log4j12
说明 slf4j 优于 log4j v1.2
您应该将 slf4j 绑定(bind)更新为
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.1</version>
</dependency>
最新版本是2.9.1。 2.9.0之前的版本 doesn't match您的 SLF4J API 版本。
关于java - Spring - SpringJUnit4ClassRunner 仍然使用 LOG4J 1 而不是 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041518/