Switch Spring Boot 2.x from Logback to Log4j2: Step‑by‑Step Guide

Learn how to replace Spring Boot 2.x's default Logback logging with the high‑performance Log4j2 by adding the starter dependency, configuring the logging file, and setting up a basic log4j2.xml, including code snippets and troubleshooting tips for a smooth migration.

Programmer DD
Programmer DD
Programmer DD
Switch Spring Boot 2.x from Logback to Log4j2: Step‑by‑Step Guide

In the previous article we introduced the default Logback logging framework in Spring Boot 2.x. This tutorial shows how to replace Logback with Log4j2, which remains the fastest logging framework despite recent vulnerabilities.

Hands‑on Trial

Below are the steps to integrate Log4j2 into any Spring Boot 2.x project.

Step 1: Add the Log4j2 starter dependency in pom.xml and exclude the default spring-boot-starter-logging dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Step 2: In application.properties set the logging configuration file location: logging.config=classpath:log4j2.xml Step 3: Create log4j2.xml under src/main/resources (or the path defined above) with a basic configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

This simple configuration is enough to get the application running. For advanced Log4j2 settings, refer to the official documentation.

FAQ

How to verify that Log4j2 is actually being used? Run the application in debug mode and check the logger implementation. The screenshots below illustrate the difference between Logback and Log4j2 output.

Logback output
Logback output
Log4j2 output
Log4j2 output

Because Log4j2 had a serious vulnerability in the past, ensure you use version 2.17.0 or newer (Spring Boot 2.6.2+ already includes 2.17.0). The latest version is 2.17.1; upgrade instructions are available in the referenced article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaloggingSpring BootTutoriallog4j2
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.