【问题标题】:SpringJUnit4ClassRunner plus Log4j2 plus log4j2.xml outside classpathSpringJUnit4ClassRunner 加 Log4j2 加 log4j2.xml 在类路径之外
【发布时间】:2015-07-11 20:26:14
【问题描述】:

我有一些使用SpringJUnit4ClassRunner 运行的测试。我也刚迁移到 Log4j2,现在我无法加载我的 log4j2.xml 配置文件,我总是收到这个错误:

ERROR StatusLogger No log4j2 configuration file found. Using default 
      configuration: logging only errors to the console.

问题是配置文件不在类路径上。相反,我将它与类路径之外的其他配置数据一起保存在一个单独的文件夹中。当我开始测试时,我已经设置了-Dconfig=/path/to/config/folder。我知道我在运行测试时也可以通过-Dlog4j.configurationFile=...,但我不想这样做。

我想要做的是使用基于 Spring 注释的配置来编写一个配置类,我在其中以编程方式设置 Log4j 的位置。如果我有web application,这是可能的,但我不知道如何将其应用于SpringJUnit4ClassRunner

@Configuration
public class Log4jConfiguration {
    /** Initialize Log4j2 with a custom location for the log4j2.xml **/
}

【问题讨论】:

  • 使用-Dlog4j.configurationFile=... 与您尝试使用-Dconfig=/path/to/config/folder 达到的效果完全相同。那么,为什么不使用第一个选项并依赖自动配置呢?在这件事上你真的不需要重新发明轮子
  • 因为我在那个文件夹里还有其他的配置文件,而且我的代码里很多地方都依赖这个,所以不想改了。

标签: spring log4j2


【解决方案1】:

可能是您的 Maven,请指定:

<resources>
    <resource>
        <directory>${basedir}/src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include><!---->
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

检查一下。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多