Partner – Trifork – NPI (cat=Spring Boot)
announcement - icon

Navigating the complexities of Spring can be difficult, even for seasoned developers.

If you need direct, practical help and guidance with your own Spring work, Trifork's CTO, Joris Kuipers, is running a closed-door call.

It's free, but it's limited to only 3 seats, so if you need it, I would join quickly and be sure to attend:

>>> CTO Spring Open Office Hour Session - Technical Guidance

With more than 15 years of leading custom software development projects involving Spring, Joris has gained a lot of real-world experience, and this call is about sharing and helping the community.

Enjoy.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!