How to configure Apache Log4j2 with Spring Boot

Apache Log4j2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.The tutorial will introduce way to configure Log4j2 with Spring Boot

Related Posts:
Logging with SLF4J
Logging with SLF4J and Logback

I.Technology for Apache Log4j2 tutorial

– Java 8
– Maven 3.6.1
– Spring Boot: 1.5.9.RELEASE
– Spring Tool Suite – Version 3.9.0.RELEASE

II. Overview Spring Log4j2
1. Structure of Project


2. Step To Do

– Create Spring Boot project.
– Configure dependency for Log4j2
– Create a SimpleController.
– Configure log4j2.xml file
– Build & Run

III. Practice
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, Input all needed information:


Press Next then press Finish, project will be created successfully.

2. Configure Spring Boot Log4j2 dependencies

– Exclude logback from default log dependency of Spring Boot


– Add Spring Web MVC dependency:


– Add Log4j2 dependency


3. Create a Simple Controller

We define a logger which is ready for showing messages with 4 mode: Debug, Info, Warn, Error

package com.javasampleapproach.log4j2.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class SimpleController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    String hello(){
        logger.debug("Debug message");"Info message");
        logger.warn("Warn message");
        logger.error("Error message");
        return "Done";
4. Configure Log4j2.xml

– Create a log4j2.xml file for configuration log4j2 (Spring will understand that the “log4j2.xml” file is a configuration for log4j2).

– Create 3 Loggers:




– With 3 Appenders:


				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n


– Full Details:

					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

5. Build & Run

– Build Spring Boot project with goals:
clean install

– Run Spring Boot project with mode: Spring Boot App
Because levellog = INFO so DEBUG log is missed, Result is:

[INFO ] 2017-12-05 18:17:57.358 [http-nio-8080-exec-1] SimpleController - Info message
[WARN ] 2017-12-05 18:17:57.359 [http-nio-8080-exec-1] SimpleController - Warn message
[ERROR] 2017-12-05 18:17:57.359 [http-nio-8080-exec-1] SimpleController - Error message
IV. Sourcecode


By grokonez | September 10, 2016.

Last updated on December 5, 2017.

Related Posts

6 thoughts on “How to configure Apache Log4j2 with Spring Boot”

  1. How did you run this application?
    As a “mvn clean package spring-boot:run” or “mvn clean package” and then “java -jar projectname.war”.
    Basically, what i want to know is the above wont update the log files, if you deploying this war file to a container like docker or running it as a “java -jar projectname.war”

  2. How to use JDBC appender in this example. I need to pass some custom value to the database columns. Please help me out on this.

Got Something To Say:

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