Log4j2 通过根类路径中的 log4j2.xml
配置文件与 Spring Boot 完美配合,正如文档所述。
虽然尝试将此文件移动到其他位置时,我无法在启动时将新位置传递给 Spring。来自 the documentation :
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 系统属性设置新位置
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.
最佳答案
如 Spring reference documentation 中所述, logging.config
属性不能在应用程序属性中设置,因为它们是在日志记录初始化后读取的。
解决方案是通过这种方式提供外部日志记录配置的路径:
java -Dlogging.config='/path/to/log4j2.xml' -jar app-current.jar
关于java - 如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28349309/