java 如何在测试期间覆盖 log4j.properties?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4106524/
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
How to override log4j.properties during testing?
提问by yegor256
I'm trying to log all DEBUG
messages to console during testing in maven. For this purpose I created a file src/test/resources/log4j.properties
, which is going to override the configuration I already have in src/main/resources/log4j.properties
. Unfortunately such an overriding is not happening. Why and how to fix it?
我试图DEBUG
在 maven 测试期间将所有消息记录到控制台。为此,我创建了一个文件src/test/resources/log4j.properties
,该文件将覆盖我已有的配置src/main/resources/log4j.properties
。不幸的是,这种覆盖并没有发生。为什么以及如何修复它?
采纳答案by yegor256
It should work as it is, and it works. The problem is somewhere else.
它应该可以正常工作,并且可以正常工作。问题出在其他地方。
ps. I had a mess with loggers in my classpath: jog4j, slf4j, logback (from other dependencies). As I understand, all of them are in conflict. I didn't clean this mess yet, and I still don't know how to make all packages to use one logging facility and one configuration.
附:我的类路径中的记录器乱七八糟:jog4j、slf4j、logback(来自其他依赖项)。据我了解,所有这些都存在冲突。我还没有清理这个烂摊子,我仍然不知道如何让所有的包都使用一种日志记录工具和一种配置。
回答by Robert Munteanu
Rename your test configuration file to e.g. log4j-surefire.properties
and configure log4j to pick it up during surefire execution:
将您的测试配置文件重命名为 eglog4j-surefire.properties
并配置 log4j 以在surefire执行期间获取它:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:${project.build.testOutputDirectory}/log4j-surefire.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>