Adding Log4j Support for spring boot web application

Sominda Gamage
4 min readMar 19, 2021

--

Logs are an essential part of troubleshooting applications. Logs help us to get an understanding of how our applications are running various infrastructure components, various scenarios, etc. So adding proper logs to your spring application will help you,

  • understand how the application works in the initial developing stages and against different scenarios.
  • mainly, will improve the maintenance experience when it has been deployed in a production environment.

In this blog, I will explain how to use log4j2 in your spring boot application. For this article, I will use the artifacts from my previous blog.

As per my previous blog, when using starters, Logback is used for logging by default. So the first step is to exclude the Logback dependencies from the starters and then add log4j2 starter dependencies to this application via the pom.xml. So I will modify the starter dependency to exclude logging and add a new dependency.

There are several ways that you can configure log4j2 in your application.

  • Using XML configuration file
  • Using a properties file
  • Using JSON format

In this blog, I will explain how to enable log4j2 via the first two methods.

Configuring via an XML file

First, navigate to the resources directory in the project and add an XML file as log4j2.xml. We need to stick to this name since then only Spring can automatically detect the log4j2 configurations and configure the application.

Now let's add some sample configuration to the XML file.

So here I have added a log pattern, defined a root logger, and added an appender to my base package. Now let's go ahead and add some logs. To make things easy, I will add a new controller to demonstrate the logging.

In the above controller, I have added a new method and mapped it a new endpoint. Also, I have created a static Logger instance. Let’s go ahead and start the application and visit http://localhost:8080/log/print. Once you have seen the success message in the browser, check the terminal for the logs.

The sample project for the above part can be found here.

Configuring via log4j2.properties file

Out of the many methods, this is my favorite and this is the method that I would like to recommend to others as well.

In this approach the only difference is, having a log4j2.properties file instead of the log4j2.xml file. Let's add the log4j2.properties file to the resources directory.

In this properties file, I have saved the logs in a directory with the name logs and also I have specified a file name to be saved as sample-application.log. Also, I have defined a separate console to print the logs and I have set the root logger level to Info.

To demonstrate the feature, I have modified the displayLogs as follows.

Let’s go ahead and start the application and visit log/print endpoint. Once you visit the endpoint from the browser, notice the following logs in the terminal window.

Notice that the debug logs are not displayed. To display the logs, you can uncomment line from 34–37 in the log4j2.properties and re-run the application.

In this approach, the application uses the log4j2.properties file, that is in the resources directory. So when we need to change the properties file, we need to build the whole application again from the scratch and this is not the way to change the log4j2 configs. So the recommended approach is to point to an external log4j2.properties using the following command.

java -Xms128m -Xmx256m -Dlog4j.configurationFile=<path-to-file>/log4j2.properties -jar sample-spring-app-1.0-SNAPSHOT.war

So yeah, that is how to add log4j2 to your spring application. Sample code for adding log4j2.properties file can be found here.

Don’t forget to applause if you have learned something from this blog. From the next blog, I will try to introduce you to the filters and how to intercept requests from filters.

--

--