Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Log4J logs during maven test phase

Tags:

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)

like image 337
Luigi R. Viggiano Avatar asked Dec 18 '12 15:12

Luigi R. Viggiano


People also ask

Does Maven use log4j?

As you can see in this answer, maven supports SLF4J logging. If you just add the Log4j to SLF4j adapter to the plugin. By adding this dependency, log4j will redirect to SLF4j and SLF4j redirects to the maven logging.


2 Answers

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.

like image 95
Luigi R. Viggiano Avatar answered Sep 28 '22 02:09

Luigi R. Viggiano


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.

like image 30
Qwerky Avatar answered Sep 28 '22 01:09

Qwerky