Log4J2 is an upgrade to the popular Log4J logging framework. It is one of the newer frameworks and its goal is to provide improvements on Logback as well as Log4J. Also, it aims to avoid some of the issues in its older version. In this post, we will look at Spring Boot Log4J2 setup.

In case you are new to Spring Boot and would like to know more, please refer to a detailed post about Spring Boot Microservices.

1 – Maven Dependencies

To import Log4J2 in our Spring Boot application, we need to first exclude the default logging setup that comes along with Spring Boot.

Below is how we can do so:

<!-- Exclude Spring Boot's Default Logging -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>

		<!-- Add Log4j2 Dependency -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

As we can see, we are excluding the spring-boot-starter-logging and adding the dependency for spring-boot-starter-log4j2.

2 – Using Log4J2 in the Application

This bare minimum setup itself is enough to use Log4J2 in our Spring Boot application.

Below is an example on how to log messages and data in the application.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    private static final Logger LOG = LogManager.getLogger(DemoController.class);

    @GetMapping(value = "/api/greeting/{name}")
    public ResponseEntity<?> getGreetingMessage(@PathVariable(value = "name") String name) {
        LOG.info("Get Greeting Message Input: " + name);
        String greetingMessage = "Hello and Welcome " + name;
        LOG.debug("Greeting Message Output: " + greetingMessage.toUpperCase());
        return ResponseEntity.ok().body(greetingMessage);
    }
}

We first create a private static final instance of the Logger using LogManager.

Next, we go log different messages using LOG.info and LOG.debug. If we run the application now, we will see the logs registered using LOG.info and not the LOG.debug messages. This is because, by default, the Log Level is INFO.

3 – Understanding Log Levels

There are several Log Levels that we can leverage in order to have an effective logging mechanism.

The levels are TRACE, DEBUG, INFO, WARN, ERROR and FATAL. The way the logging works is that all the logs generated with a level greater than or equal to the current setup level will be logged.

So for example, if the current log level is INFO, all the log generated with LOG.info, LOG.warn, LOG.error and LOG.fatal will be logged. If we set the current Log Level to DEBUG, then all logs for LOG.debug and above will be logged and so on. See below example code:

@RestController
public class DemoController {

    private static final Logger LOG = LogManager.getLogger(DemoController.class);

    @GetMapping(value = "/api/greeting/{name}")
    public ResponseEntity<?> getGreetingMessage(@PathVariable(value = "name") String name) {
        LOG.info("Get Greeting Message Input: " + name);
        String greetingMessage = "Hello and Welcome " + name;
        LOG.debug("Greeting Message Output: " + greetingMessage.toUpperCase());
        LOG.warn("Warning");
        LOG.error("Error");
        LOG.fatal("Fatal");
        return ResponseEntity.ok().body(greetingMessage);
    }
}

In this case, we use LOG.warn, LOG.error and LOG.fatal to log some messages.

4 – Setting Log Level in Spring Boot Log4J2

Log Level can be setup directly in the application.properties. For example, in order to setup a root logging level for the entire application we can add a property as below to our application.properties file.

logging.level.root = debug

We can also have more fine-grained control over our logging. For example, if we want a certain log level to be only applicable for our application code packages, we could add a property as below:

logging.level.com.progressivecoder.loggingdemo = debug

In this case, the Log Level DEBUG will only work for classes within the package com.progressivecoder.loggingdemo.


With this, we have successfully looked at Spring Boot Log4J2 setup. The code for the examples are available on Github. You can read more about Log4J2 on the official Apache website.

In the next post, we will take a more in-depth look into configuring LOG4J2 using XML/YAML configuration files. Also, we will look at appending logs in files.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *