在maven测试阶段禁用Log4J日志 [英] Disabling Log4J logs during maven test phase
问题描述
在IDE中进行开发时,跟踪和调试日志会很有帮助,但在构建期间,我发现这些行非常令人不安,并且混淆了由maven或其他构建工具打印出来的报告。
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.
让log4j尊重像 -Dlog4j.rootLogger = OFF
1 这样的系统属性与maven一起使用会很好不需要更改项目文件的东西。
我知道我可以指定 -Dlog4j.configuration = alternateconfig.props
2 但是我在这里要求找出是否有人发现了一种更智能的方法,可以在构建过程中禁即一些java类检测maven作为调用者,禁用log4j或其他智能解决方案。
It would be nice to have log4j honoring a system property like -Dlog4j.rootLogger=OFF
1 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.
任何提示?
备注:
[1]:已经尝试了,但它不起作用。
[1]: already tried, and it doesnt work.
[2]:这很不错,但它似乎不适用于maven(也许肯定会跳过它)
[2]: that's pretty good, but it doesn't seem to work well with maven (maybe surefire skips it)
推荐答案
正如<$ c $所解释的那样c> @artbristol (对问题的评论)这可以在surefire中配置。
可以在pom.xml中以这种方式完成:
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>
然后,让文件 $ {basedir} / etc / log4j-silent .properties
使用以下设置可以解决问题:
Then, having the file ${basedir}/etc/log4j-silent.properties
with following settings does the trick:
log4j.rootLogger=OFF
在maven的测试运行期间,log4j被完全禁用,并且一切都在IDE中正常工作。
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.
这篇关于在maven测试阶段禁用Log4J日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!