如何在 Java Spring Boot 中更改 log4j2.xml 的默认位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28349309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the default location of log4j2.xml in Java Spring Boot?
提问by micpalmia
Log4j2 is working nicely with Spring Boot through the log4j2.xml
configuration file in the root classpath, exactly as the documentation states.
Log4j2 通过log4j2.xml
根类路径中的配置文件与 Spring Boot 很好地协同工作,正如文档所述。
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。从文档:
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
.
可以通过在类路径中包含适当的库来激活各种日志系统,并通过在类路径的根目录中或在 Spring Environment 属性指定的位置
logging.config
提供合适的配置文件来进一步定制。
I tried setting the new location with a Java system property
我尝试使用 Java 系统属性设置新位置
java -jar -Dlogging.config="classpath:/config/log4j2.xml" target/app.jar
or using an external application.properties
containing the relevant property
或使用application.properties
包含相关属性的外部
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.
采纳答案by micpalmia
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.
正如Spring 参考文档中所指定的,logging.config
不能在应用程序属性之间设置该属性,因为它们是在日志记录初始化之后读取的。
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
回答by konqi
The answer of micpalmiais absolutely correct.
micpalmia的回答是绝对正确的。
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:
我需要将配置放在类路径之外,我不想将配置文件作为参数传递。因此,我在类路径资源中放置了一个非常简单的日志记录配置,并在启动时让 Spring Boot 应用程序重新配置日志记录,如下所示:
@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.
这种方法有一个明显的缺点:整个应用程序启动过程不会被记录为外部配置。但是一旦自定义代码运行,记录器就会按预期工作。虽然你可能不会,但我发现这是我可以接受的妥协。
回答by Harry.Chen
I have the same problem in my project, besides log4j2.xml I also need other config files in the class path. Here is my 2 solutions that works:
我的项目中有同样的问题,除了 log4j2.xml 我还需要类路径中的其他配置文件。这是我的 2 个有效的解决方案:
Soluation 1 : Start spring boot application with org.springframework.boot.loader.JarLauncher
解决方案 1:使用 org.springframework.boot.loader.JarLauncher 启动 spring boot 应用程序
java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher
Solution 2: Write a './config' class path entry in the MANIFEST.MF in the jar
解决方案二:在jar中的MANIFEST.MF中写入'./config'类路径项
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>./config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by SachinVsSachin
回答by Atul
In case of property file:
如果是属性文件:
java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar
java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar
To the command line works in Spring Boot 2. Don't forget to add file:before the path.
命令行在 Spring Boot 2 中有效。不要忘记在路径之前添加file:。
回答by Daud
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:
正如此处的 log4j2 文档中所述,您可以log4j2.component.properties
在资源文件夹(或类路径中的任何位置)中包含一个名为的文件,在该文件中,您可以提供文件位置的名称(或新文件名),如下所示:
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
您也可以通过此处提到的context-param
字段提供配置文件位置,但我还没有尝试过该选项web.xml
(This works with Spring Boot too)
(这也适用于 Spring Boot)