如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置? [英] How can I change the default location of log4j2.xml in Java Spring Boot?

查看:48
本文介绍了如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Log4j2 is working nicely with Spring Boot through the log4j2.xml configuration file in the root classpath, exactly as the documentation states.

虽然尝试将此文件移动到其他位置时,我无法在启动时将新位置传递给 Spring.来自 文档:

When trying to move this file to a different location though, I'm not able to pass the new location to Spring at startup. From the documentation:

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

The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config.

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

I tried setting the new location with a Java system property

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

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

or using an external application.properties containing the relevant property

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

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

But I am regularly greeted by the following error message.

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

推荐答案

Spring 参考文档logging.config 属性不能在应用程序属性中设置,因为它们是在之后读取的日志记录已经初始化.

As specified in the Spring reference documentation, the logging.config property cannot be set among the application properties, as they are read after the logging has already been initialised.

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

The solution is to provide the path to the external logging configuration this way:

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

这篇关于如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆