Sub topic 14
Switching the backend to Log4j2
This chapter assumes you know how SLF4J and Logback fit together in Spring Boot, and that you can edit a Maven pom.xml. If those ideas are new, review the chapter on how Spring Boot logging is assembled first.
Why you might switch
Spring Boot uses Logback by default. Your code calls the SLF4J API, so the backend can change without touching a single log statement. Teams usually switch to Log4j2 for one of these reasons:
- The company already standardizes on Log4j2 configuration and tooling.
- You want Log4j2's asynchronous loggers for high-throughput services.
- You need a Log4j2 feature, such as a specific appender or lookup.
If none of these apply, stay with Logback. It needs no extra setup and has the most documentation in the Spring ecosystem.
What changes and what stays the same
Only the implementation changes. Your Logger and LoggerFactory imports stay as org.slf4j. Properties such as logging.level.*, logging.file.name, and logging.group.* keep working, because Spring Boot's logging system supports Log4j2 too.
Swapping the dependency
Every Spring Boot starter pulls in spring-boot-starter-logging, which brings Logback. To replace it, exclude that starter and add spring-boot-starter-log4j2.
<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>The exclusion must appear on every starter that brings in the logging starter. If you use spring-boot-starter-data-jpa or spring-boot-starter-actuator, they also depend on it, so exclude it there too. A simpler approach in Maven is to declare the exclusion once on each direct starter you use, or in Gradle to exclude the module from all configurations.
Configuring Log4j2
Spring Boot looks for a Log4j2 configuration file at the root of the classpath. It prefers log4j2-spring.xml, which lets Spring Boot control initialization, over plain log4j2.xml. Without either file, Boot applies its own defaults and your logging.* properties.
Do not leave a logback-spring.xml in the project and expect it to work. Log4j2 ignores it, so any appenders and profile logic in it must be rewritten in the Log4j2 format.
Common errors
- Both backends on the classpath. Boot warns about multiple SLF4J bindings, or logging fails at startup. Run
mvn dependency:treeand look forlogback-classicafter the switch. Any starter still pulling it in needs the exclusion. - Logback-specific properties. Settings such as
logging.logback.rollingpolicy.max-file-sizeapply only to Logback. For Log4j2, configure rolling inlog4j2-spring.xml. - Unchanged output after editing config. Confirm the file name is exactly
log4j2-spring.xmland sits insrc/main/resources.
Exercise
You excluded spring-boot-starter-logging from spring-boot-starter-web and added spring-boot-starter-log4j2. At startup you still see a warning about multiple SLF4J bindings. What is the most likely cause, and how do you find it?
Show answer
Another starter, such as actuator or data-jpa, still brings in spring-boot-starter-logging, so Logback remains on the classpath. Run mvn dependency:tree and search for logback-classic or spring-boot-starter-logging, then add the exclusion to the starter that pulls it in.