Disabling Log4J logs during maven test phase

前端 未结 3 1031
醉酒成梦
醉酒成梦 2021-01-03 22:04

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 m

相关标签:
3条回答
  • 2021-01-03 22:28

    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.

    0 讨论(0)
  • 2021-01-03 22:35

    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.

    0 讨论(0)
  • 2021-01-03 22:37

    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.

    0 讨论(0)
提交回复
热议问题