1. Overview
In our previous tutorial on Logging in Spring Boot, we showed how to use Log4j2 in Spring Boot.
In this short tutorial, we'll learn how to change the default location of the Log4j2 configuration file.
2. Use Properties File
By default, we'll leave the Log4j2 configuration file (log4j2.xml/log4j2-spring.xml) in the project classpath or resources folder.
We can change the location of this file by adding/modifying the following line in our application.properties file:
logging.config=/path/to/log4j2.xml
3. Use VM Options
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
Finally, we can programmatically configure the location of this file by changing our Spring Boot Application class like this:
@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.
5. Conclusion
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.