如何在 Java Spring 引导中更改 log4j2.xml 的默认位置?
How can I change the default location of log4j2.xml in Java Spring Boot?
Log4j2 与 Spring 通过根类路径中的 log4j2.xml
配置文件引导很好地工作,正如文档所述。
虽然尝试将此文件移动到其他位置,但我无法在启动时将新位置传递给 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
micpalmia的回答完全正确。
我需要将配置放在类路径之外我不想将配置文件作为参数传递。所以我在类路径资源中放置了一个非常简单的日志记录配置,并让 spring 引导应用程序在启动时重新配置日志记录,如下所示:
@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.xml我还需要class路径下的其他配置文件。
这是我的 2 个有效的解决方案:
解决方案 1:使用 org.springframework.boot.loader.JarLauncher
启动 spring 启动应用程序
java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher
解决方案 2:在 jarMANIFEST.MF 中写一个 './config' class 路径条目
<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>
我有设置自定义路径或更改日志文件的现有文件路径的有效解决方案。如果您配置了 log4j2.xml 文件,请打开它并查看您必须在何处对配置日志文件路径进行一行更改。
如果是 属性 文件:
java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar
命令行在 Spring Boot 2 中工作。不要忘记在路径前添加 file:。
如 log4j2 文档 here 中所述,您可以在资源文件夹(或类路径中的任何位置)中包含一个名为 log4j2.component.properties
的文件,并在该文件中提供名称文件位置(或新文件名)如下:
log4j.configurationFile=path/to/log4j2.xml
或
log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
您也可以通过 web.xml
的 context-param
字段提供配置文件位置,如前所述 here,但我还没有尝试过该选项
(这也适用于 Spring Boot)
Log4j2 与 Spring 通过根类路径中的 log4j2.xml
配置文件引导很好地工作,正如文档所述。
虽然尝试将此文件移动到其他位置,但我无法在启动时将新位置传递给 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
micpalmia的回答完全正确。
我需要将配置放在类路径之外我不想将配置文件作为参数传递。所以我在类路径资源中放置了一个非常简单的日志记录配置,并让 spring 引导应用程序在启动时重新配置日志记录,如下所示:
@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.xml我还需要class路径下的其他配置文件。 这是我的 2 个有效的解决方案:
解决方案 1:使用 org.springframework.boot.loader.JarLauncher
启动 spring 启动应用程序java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher
解决方案 2:在 jarMANIFEST.MF 中写一个 './config' class 路径条目
<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>
我有设置自定义路径或更改日志文件的现有文件路径的有效解决方案。如果您配置了 log4j2.xml 文件,请打开它并查看您必须在何处对配置日志文件路径进行一行更改。
如果是 属性 文件:
java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar
命令行在 Spring Boot 2 中工作。不要忘记在路径前添加 file:。
如 log4j2 文档 here 中所述,您可以在资源文件夹(或类路径中的任何位置)中包含一个名为 log4j2.component.properties
的文件,并在该文件中提供名称文件位置(或新文件名)如下:
log4j.configurationFile=path/to/log4j2.xml
或
log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
您也可以通过 web.xml
的 context-param
字段提供配置文件位置,如前所述 here,但我还没有尝试过该选项
(这也适用于 Spring Boot)