Spring - SpringJUnit4ClassRunner仍然使用LOG4J 1而不是2?

时间:2017-10-31 17:43:53

标签: java spring maven log4j log4j2

我正在使用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>

1 个答案:

答案 0 :(得分:1)

这是旧的log4j绑定:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

此处slf4j-log4j12表示slf4j超过log4j v1.2

您应该将slf4j绑定更新为

<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版本的版本。