'How to override log4j.properties during testing?
I'm trying to log all DEBUG messages to console during testing in maven. For this purpose I created a file src/test/resources/log4j.properties, which is going to override the configuration I already have in src/main/resources/log4j.properties. Unfortunately such an overriding is not happening. Why and how to fix it?
Solution 1:[1]
Rename your test configuration file to e.g. log4j-surefire.properties and configure log4j to pick it up during surefire execution:
<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>
Solution 2:[2]
Beware of changed version for that, with 2.17.1 of log4j at least, you'll have to change it from log4j.configuration to log4j.configurationFile to avoid having
ERROR StatusLogger Reconfiguration failed: No configuration found for '18b4aac2' at 'null' in 'null'
Here an example
<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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert Munteanu |
| Solution 2 | Pipo |
