java 在 Maven 测试阶段禁用 Log4J 日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13935715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling Log4J logs during maven test phase
提问by Luigi R. Viggiano
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.
在 IDE 中进行开发时,跟踪和调试日志可能会有所帮助,但在构建过程中,我发现这些行非常令人不安,并且混淆了 maven 或其他构建工具打印的报告。
It would be nice to have log4j honoring a system property like -Dlog4j.rootLogger=OFF
1to use with maven or something which doesn't require changes on the project files.
I know I can specify the -Dlog4j.configuration=alternateconfig.props
2but 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.
让 log4j 尊重像-Dlog4j.rootLogger=OFF
1这样的系统属性以与 maven 或不需要更改项目文件的东西一起使用会很好。我知道我可以指定-Dlog4j.configuration=alternateconfig.props
2但我在这里询问是否有人找到了一种更智能的方法来在构建过程中以最少的手动干预禁用日志记录。即某些 java 类将 maven 检测为禁用 log4j 的调用者,或其他智能解决方案。
Any hint?
任何提示?
Notes:
笔记:
[1]: already tried, and it doesnt work.
[1]:已经试过了,还是不行。
[2]: that's pretty good, but it doesn't seem to work well with maven (maybe surefire skips it)
[2]:这很好,但它似乎不适用于 maven(也许 surefire 跳过了它)
回答by Luigi R. Viggiano
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:
正如@artbristol
(对问题的评论)所解释的,这可以在surefire中进行配置。在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:
然后,使用具有${basedir}/etc/log4j-silent.properties
以下设置的文件即可解决问题:
log4j.rootLogger=OFF
The log4j gets completely disabled during test runs in maven, and everything works normally in the IDE.
在 maven 中测试运行期间,log4j 被完全禁用,并且在 IDE 中一切正常。
A better solution would be not to have the additional configuration file; but can't find it so far.
更好的解决方案是没有额外的配置文件;但至今找不到。
回答by Qwerky
Specify a test log4j configuration file with no appender.
指定一个没有附加程序的测试 log4j 配置文件。
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.
例如,如果您在 中有一个 log4j.properties src/main/resources
,则将其复制到src/test/resouces
并删除附加程序或将日志级别设置为致命。
回答by amichair
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.
Maven 输出显示关于 log4j 未初始化的警告,但除此之外它似乎可以工作。