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

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

Course – LS – All

Get started with Spring and Spring Boot, 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 and Spring Boot, 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 open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.