问题描述
log4j2与root class路径中的log4j2.xml配置文件一起与弹簧启动很好地工作,完全如文档所示.
尝试将此文件移至其他位置时,我无法将新位置传递到启动时的春季.来自文档:
可以通过包括 班级路径上适当的库,并进一步定制 在classPath的根部提供合适的配置文件, 或在春季环境物业指定的位置 logging.config .
我尝试使用Java System属性设置新位置
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参考文档,logging.config属性无法在应用程序属性之间设置,因为在已初始化日志记录后会读取它们.
解决方案是提供以这种方式的外部记录配置的路径:
java -Dlogging.config='/path/to/log4j2.xml' -jar app-current.jar
其他推荐答案
如log4j2文档中提到的,您可以在资源文件夹(或类路径中的任何地方)中包含一个名为log4j2.component.properties的文件,并且可以提供类似的文件位置的名称(或一个新文件名):
log4j.configurationFile=path/to/log4j2.xml
或
log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
您可以通过context-param字段提供web.xml的context-param字段或所述在这里,但我没有尝试过该选项
(这也适用于Spring Boot)
其他推荐答案
micpalmia 的答案是绝对正确的.
我需要将配置放在class路径之外,我不想将配置文件作为参数传递.因此,我将非常简单的日志记录配置放在classPath资源中,并在开始时将弹簧启动应用程序重新配置记录,例如:
@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"); // ... } }
这种方法具有重要的缺点:整个应用程序引导过程将不会以外部配置记录.但是,一旦运行自定义代码,记录器按预期工作.虽然您可能不是,但我发现这是我可以忍受的妥协.
问题描述
Log4j2 is working nicely with Spring Boot through the log4j2.xml configuration file in the root classpath, exactly as the documentation states.
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:
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.
I tried setting the new location with a Java system property
java -jar -Dlogging.config="classpath:/config/log4j2.xml" target/app.jar
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
其他推荐答案
As mentioned in the log4j2 documentation here, you can include a file named log4j2.component.properties in your resources folder (or anywhere in the classpath) and inside that file, you can provide the name of the file location (or a new file name) like this:
log4j.configurationFile=path/to/log4j2.xml
or
log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
You can alternatively provide the config file location via the context-param fields of web.xml as mentioned here, but I haven't tried that option
(This works with Spring Boot too)
其他推荐答案
The answer of micpalmia is absolutely correct.
I needed to put the configuration outside the classpath I didn't want to pass the config file as a parameter. So i put a very simple logging configuration in the classpath resources and had the spring boot application reconfigure logging upon start, like so:
@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"); // ... } }
This approach has a significant disadvantage: The whole application boot process will not be logged as externally configured. But once the custom code is run the logger works as intended. While you may not, I find this to be a compromise I can live with.