Log4j2.xml文件未找到,但是log4j2-test.xml文件存在。

7
我正在将log4j 1.x升级到log4j 2,并已将log4j.xml配置文件更新为新的log4j2.xml配置文件。我正在运行maven、spring和mule。
在运行测试maven构建时,log4j2.xml文件没有被使用。但是,如果我将文件重命名为log4j2-test.xml,则会被使用(例如,在第二种情况下,具有自定义PatternLayout的控制台记录器可以正常工作,但在第一种情况下使用默认配置)。
为什么log4j会选择使用log4j2-test.xml而不是log4j2.xml呢?
1个回答

9

由于 log4j2-test.xml加载优先级较高,你很可能有另一个位于 classpath 上的 log4j2 配置文件覆盖了你的 log4j2.xml。尝试在运行maven测试构建时使用以下代码来查找正在获取的 conf 文件的位置:

for (String confFile : Arrays.asList("/log4j2-test.xml", "/log4j2.yaml", "/log4j2.yml", "/log4j2.json", "/log4j2.jsn", "/log4j2.xml")) {
    URL resource = YourTestClass.class.getResource(confFile);
    if (resource != null) {
        System.out.format("found %s in: %s\n", confFile, resource.getFile());
    }
}

1
谢谢@heenenee,经过这样做,我能够确定mule-core生成了自己的log4j2-test.xml文件,它具有优先权。现在我需要弄清楚如何覆盖或忽略该文件,或停止生成或将其放置在类路径上。 - Tanner Rutgers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接