java - 如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置?

标签 java log4j spring-boot log4j2

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/

相关文章:

java - 手动调用监听器方法的坏习惯

java - [Azure] [Application Insights Java代理]无法创建InProcessTelemetryChannel,为空

java - Jetty 中的 log4j 设置

java - log4j,在使用时清除日志(不仅仅是在启动时)

java - 如何获取服务器基本路径来查找和读取 json

Java java.net.ConnectException : Connection refused

java - 任务的分钟长轮询或单个调度程序 - 通知系统的方法

java - Maven 与较旧的 Spring Boot 版本的依赖关系

java - 如何根据文件大小保持滚动日志文件?

java - AngularJs $http.post 到 Spring Boot?