1. Overview
1.概述
In our previous tutorial on Logging in Spring Boot, we showed how to use Log4j2 in Spring Boot.
在我们之前关于Spring Boot中的记录的教程中,我们展示了如何在Spring Boot中使用Log4j2。
In this short tutorial, we’ll learn how to change the default location of the Log4j2 configuration file.
在这个简短的教程中,我们将学习如何改变Log4j2配置文件的默认位置。
2. Use Properties File
2.使用属性文件
By default, we’ll leave the Log4j2 configuration file (log4j2.xml/log4j2-spring.xml) in the project classpath or resources folder.
默认情况下,我们会将Log4j2配置文件(log4j2.xml/log4j2-spring.xml)留在项目classpath或resources文件夹中。
We can change the location of this file by adding/modifying the following line in our application.properties file:
我们可以通过在application.properties文件中添加/修改以下一行来改变这个文件的位置。
logging.config=/path/to/log4j2.xml
3. Use VM Options
3.使用虚拟机选项
We can also add the following VM option when running our program to achieve the same goal:
我们还可以在运行程序时添加以下虚拟机选项,以实现同样的目标。
-Dlogging.config=/path/to/log4j2.xml
4. Programmatic Configuration
4.程序化配置
Finally, we can programmatically configure the location of this file by changing our Spring Boot Application class like this:
最后,我们可以通过改变我们的Spring Boot Application类,以编程方式配置该文件的位置。
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... param) {
Configurator.initialize(null, "/path/to/log4j2.xml");
}
}
This solution has one drawback: the application boot process won’t be logged using Log4j2.
这个解决方案有一个缺点:应用程序的启动过程不会被使用Log4j2记录。
5. Conclusion
5.总结
In summary, we’ve learned different ways to change the default location of the Log4j2 configuration file in Spring Boot. I hope these things help with your work.
综上所述,我们已经学会了不同的方法来改变Spring Boot中Log4j2配置文件的默认位置。我希望这些东西对你的工作有所帮助。