如何在测试期间覆盖 log4j.properties?

问题描述 投票:0回答:3

我正在尝试在 Maven 测试期间将所有

DEBUG
消息记录到控制台。为此,我创建了一个文件
src/test/resources/log4j.properties
,它将覆盖我在
src/main/resources/log4j.properties
中已有的配置。不幸的是,这样的重写并没有发生。为什么以及如何解决它?

java logging log4j maven-2 maven-surefire-plugin
3个回答
28
投票

将您的测试配置文件重命名为例如

log4j-surefire.properties
并配置 log4j 以在 surefire 执行期间获取它:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <systemPropertyVariables>
            <log4j.configuration>file:${project.build.testOutputDirectory}/log4j-surefire.properties</log4j.configuration>
        </systemPropertyVariables>
    </configuration>
</plugin>

2
投票

它应该按原样工作,并且有效。问题出在其他地方。

附注。我的类路径中的记录器一团糟:jog4j、slf4j、logback(来自其他依赖项)。据我了解,所有这些都存在冲突。我还没有清理这个烂摊子,我仍然不知道如何让所有的包都使用一个日志记录工具和一个配置。


0
投票

注意更改版本,至少使用 log4j 的 2.17.1,你必须将它从

log4j.configuration
更改为
log4j.configurationFile
以避免

ERROR StatusLogger Reconfiguration failed: No configuration found for '18b4aac2' at 'null' in 'null'

举个例子

        <plugin>
            <!--            DOC http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
            <!-- enables java assertions for tests -->
            <configuration>
                <enableAssertions>true</enableAssertions>
                <systemPropertyVariables>
                    <log4j.configurationFile>src/test/resources/log4j2_for_surefire.xml</log4j.configurationFile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
© www.soinside.com 2019 - 2024. All rights reserved.