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

查看:938
本文介绍了如何在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.

推荐答案

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天全站免登陆