How to Use Log4j2 in Spring Boot: Setup and Example
To use
Log4j2 in a Spring Boot project, exclude the default spring-boot-starter-logging and add spring-boot-starter-log4j2 dependency. Then, create a log4j2.xml configuration file in src/main/resources to customize logging behavior.Syntax
To enable Log4j2 in Spring Boot, you need to add the Log4j2 starter dependency and exclude the default logging starter. Then, configure Log4j2 using an XML file.
spring-boot-starter-log4j2: Adds Log4j2 support.- Exclude
spring-boot-starter-logging: Prevents conflicts with default Logback. log4j2.xml: Defines logging levels, appenders, and formats.
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Exclude default logging in your main starter -->
<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>Example
This example shows a Spring Boot application using Log4j2 for logging. It includes the necessary dependencies, a simple log4j2.xml configuration, and a sample component that logs messages at different levels.
xml/java
/* pom.xml dependencies snippet */ <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> /* src/main/resources/log4j2.xml */ <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <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> /* Sample Spring Boot component */ package com.example.demo; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class LogExample implements CommandLineRunner { private static final Logger logger = LogManager.getLogger(LogExample.class); @Override public void run(String... args) { logger.info("Info level log from Log4j2"); logger.error("Error level log from Log4j2"); } }
Output
12:00:00.000 [main] INFO com.example.demo.LogExample - Info level log from Log4j2
12:00:00.001 [main] ERROR com.example.demo.LogExample - Error level log from Log4j2
Common Pitfalls
Common mistakes when using Log4j2 in Spring Boot include:
- Not excluding
spring-boot-starter-logging, causing conflicts with Logback. - Placing
log4j2.xmlin the wrong directory (must besrc/main/resources). - Incorrect XML syntax in
log4j2.xmlleading to startup errors. - Using old Log4j 1.x classes instead of Log4j2 APIs.
xml
/* Wrong: Missing exclusion causes Logback to load */ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> /* Right: Exclude default logging to avoid conflicts */ <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
Quick Reference
- Dependency: Use
spring-boot-starter-log4j2and excludespring-boot-starter-logging. - Config file: Place
log4j2.xmlinsrc/main/resources. - Logger usage: Use
org.apache.logging.log4j.LogManagerandLoggerclasses. - Log levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL.
Key Takeaways
Always exclude spring-boot-starter-logging to avoid conflicts with Logback.
Add spring-boot-starter-log4j2 dependency to enable Log4j2 in Spring Boot.
Place log4j2.xml in src/main/resources to configure logging behavior.
Use LogManager and Logger from org.apache.logging.log4j for logging calls.
Check XML syntax carefully to prevent startup errors.