【问题标题】:How can I change the default location of log4j2.xml in Java Spring Boot?如何更改 Java Spring Boot 中 log4j2.xml 的默认位置?
【发布时间】:2015-04-05 14:58:04
【问题描述】:

Log4j2 通过根类路径中的 log4j2.xml 配置文件与 Spring Boot 完美配合,正如文档所述。

但是,当尝试将此文件移动到其他位置时,我无法在启动时将新位置传递给 Spring。来自the documentation

各种日志系统可以通过包含 类路径上的适当库,并由 在类路径的根目录中提供合适的配置文件, 或 在 Spring Environment 属性指定的位置 logging.config.

我尝试使用 Java 系统属性设置新位置

java -jar -Dlogging.config="classpath:/config/log4j2.xml" target/app.jar

或使用包含相关属性的外部application.properties

logging.config=classpath:/config/log4j2.xml

但我经常收到以下错误消息。

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

【问题讨论】:

  • 您能否确认/config 是否在类路径中?如果您使用的是基于 maven 的项目,请将 xml 文件放在 src/main/resources
  • config 文件夹(包)在类路径中,它已经包含application.yml,在应用程序中被正确提取并成功使用。
  • 能否请您将xml文件放入src/main/resources并尝试以便我们缩小问题范围?
  • 如果我将 XML 文件放在正确的位置,即类路径根 (src/main/java),它可以正常工作。通过将 XML 放在 src/main/resources 文件夹中,我没有得到我们应该检查的内容。我想要做的就是把 log4j2.xml 文件放在我想要的任何地方。
  • 既然你把它放在src/main/java里面就可以正常工作了,说明配置没有问题。如果您希望它与log4j2.xml 的位置无关,则必须确保该文件夹位于类路径中。如果你使用 eclipse,right click on the project -> Build Path -> Configure Build Path 设置类路径。

标签: java log4j spring-boot log4j2


【解决方案1】:

正如Spring reference documentation 中所指定的,logging.config 属性不能在应用程序属性中设置,因为它们是在日志记录已经初始化之后读取的。

解决方案是以这种方式提供外部日志配置的路径:

java -Dlogging.config='/path/to/log4j2.xml' -jar app-current.jar

【讨论】:

  • 通过 log4j2.component.properties 文件还有另一种解决方案,如下面的answer 所述,不需要通过系统属性或命令行提供配置文件名参数
  • 从我读到的文档中:The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config. 唯一的例外是`从 Spring @Configuration 文件中的 @PropertySources 进行日志记录`,这是由初始化顺序决定的。那么,为什么“应用程序属性中不能设置logging.config属性”呢?
  • this答案
【解决方案2】:

micpalmia的答案绝对正确。

我需要将配置放在类路径之外我不想将配置文件作为参数传递。因此,我在类路径资源中放置了一个非常简单的日志记录配置,并让 Spring Boot 应用程序在启动时重新配置日志记录,如下所示:

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... param) throws UnsupportedEncodingException {
        Configurator.initialize(null, "config/log4j2.xml");
        // ...
    }
}

这种方法有一个明显的缺点:整个应用程序启动过程不会被记录为外部配置。但是一旦自定义代码运行,记录器就会按预期工作。虽然您可能不会,但我发现这是我可以接受的妥协。

【讨论】:

  • 关于启动过程,不会同时使用-Dlog4j.configurationFile-Dlogging.config 来解决这个问题吗? (它对我有用,但我不确定我们是否有相同的用例。)
  • 也检查这个答案。 link
【解决方案3】:

我的项目中有同样的问题,除了 log4j2.xml 我还需要类路径中的其他配置文件。 这是我的 2 个可行的解决方案:

解决方案 1:使用 org.springframework.boot.loader.JarLauncher 启动 spring boot 应用程序

java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher

解决方案2:在jar的MANIFEST.MF中写一个'./config'类路径条目

    <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifestEntries>
               <Class-Path>./config/</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.3.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

【讨论】:

    【解决方案4】:

    我有工作解决方案来设置自定义路径或更改日志文件的现有文件路径。如果你配置了 log4j2.xml 文件,打开它,看看你需要在哪里做一行更改配置日志文件路径。

    【讨论】:

    • 这不能回答问题。问题是询问 log4j 配置文件 (log4j.xml) 的位置,而不是用于将日志语句写入的文件
    【解决方案5】:

    如果是属性文件:

    java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar

    到命令行在 Spring Boot 2 中工作。不要忘记在路径前添加 file:

    【讨论】:

      【解决方案6】:

      如 log4j2 文档 here 中所述,您可以在资源文件夹(或类路径中的任何位置)中包含一个名为 log4j2.component.properties 的文件,在该文件中,您可以提供文件位置的名称(或新文件名)像这样:

      log4j.configurationFile=path/to/log4j2.xml
      

      log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
      

      您也可以通过web.xmlcontext-param 字段提供配置文件位置,如提到的here,但我没有尝试过该选项

      (这也适用于 Spring Boot)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-01
        • 2015-11-03
        • 2020-12-12
        • 2021-04-17
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 2020-05-22
        相关资源
        最近更新 更多