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

时间:2010-11-05 13:59:33

标签: java maven-2 logging log4j

我正在尝试在maven测试期间将所有DEBUG消息记录到控制台。为此,我创建了一个文件src/test/resources/log4j.properties,它将覆盖我在src/main/resources/log4j.properties中已有的配置。不幸的是,这种压倒一切都没有发生。为什么以及如何解决它?

2 个答案:

答案 0 :(得分:26)

将测试配置文件重命名为例如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>

答案 1 :(得分:3)

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

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