Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 943
yegor256 Avatar asked Nov 05 '10 13:11

yegor256


People also ask

How do I change the log4j properties at runtime?

Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.

Can we use log4j properties in log4j2?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".


2 Answers

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>
like image 187
Robert Munteanu Avatar answered Oct 18 '22 14:10

Robert Munteanu


It should work as it is, and it works. The problem is somewhere else.

ps. I had a mess with loggers in my classpath: jog4j, slf4j, logback (from other dependencies). As I understand, all of them are in conflict. I didn't clean this mess yet, and I still don't know how to make all packages to use one logging facility and one configuration.

like image 25
yegor256 Avatar answered Oct 18 '22 14:10

yegor256