Disabling Log4J logs during maven test phase

22,087

Solution 1

As explained by @artbristol (comment on the question) this can be configured in surefire. It can be done in this way in the pom.xml:

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <forkMode>always</forkMode>
                <systemPropertyVariables>
                    <log4j.configuration>file:${basedir}/etc/log4j-silent.properties</log4j.configuration>
                </systemPropertyVariables>
            </configuration>
        </plugin>

Then, having the file ${basedir}/etc/log4j-silent.properties with following settings does the trick:

log4j.rootLogger=OFF

The log4j gets completely disabled during test runs in maven, and everything works normally in the IDE.

A better solution would be not to have the additional configuration file; but can't find it so far.

Solution 2

Specify a test log4j configuration file with no appender.

For example if you have a log4j.properties in src/main/resources, then copy it to src/test/resouces and either remove the appender or set the log level to fatal.

Solution 3

Based on the previous answers, I use:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <forkMode>always</forkMode>
        <argLine>-Dlog4j.configuration=</argLine>
    </configuration>
</plugin>

The Maven output shows a warning about log4j not being initialized, but other than that it seems to work.

Share:
22,087
Luigi R. Viggiano
Author by

Luigi R. Viggiano

Software Engineer, Java and all related technologies, occasional blogger, you can follow me on Twitter @lviggiano, sometime I drop some code on github. If you use Java Properties files, try this: owner library

Updated on January 18, 2020

Comments

  • Luigi R. Viggiano
    Luigi R. Viggiano over 4 years

    Trace and debug logs can be helpful while doing development in the IDE, but during the build I find those lines quite disturbing, and obfuscating the report printed out by maven or other build tools.

    It would be nice to have log4j honoring a system property like -Dlog4j.rootLogger=OFF1 to use with maven or something which doesn't require changes on the project files. I know I can specify the -Dlog4j.configuration=alternateconfig.props 2 but I'm asking here to find out if somebody found a smarter way to disable logging during the build process with minimal manual intervention. I.e. some java class detecting maven as caller that disables log4j, or other smart solutions.

    Any hint?

    Notes:

    [1]: already tried, and it doesnt work.

    [2]: that's pretty good, but it doesn't seem to work well with maven (maybe surefire skips it)

  • Luigi R. Viggiano
    Luigi R. Viggiano over 11 years
    that would disable the logging also in the IDE (it does in IntelliJ IDEA, I suppose it doesn't instead in eclipse)
  • Jazzepi
    Jazzepi over 10 years
    I used your silent logger method by putting a log4j.properties file in my test/resources/ directory with the rootLogger=OFF line in it. It gets copied into the classpath of the tests and picked up. No complaints about no logger config, and no logger output :)
  • Evan
    Evan over 5 years
    What worked for me was <log4j.configurationFile>file:${project.build.testOutputDire‌​ctory}/etc/log4j2-te‌​st-silent.xml</log4j‌​.configurationFile>. Also when using IntelliJ, I wanted to see my log output in the console when running the tests through the IDE. For that I had to clear an option in Settings | Build, Execut... | Build Tools | Maven | Running Tests | Pass to JUnit...
  • FuryComputers
    FuryComputers about 3 years
    With this minor tweak:<argLine>-Dlog4j.configurationFile=</argLine> this fix worked for me.