Spring-Boot logging with log4j2?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot defaults to Logback, but it routes application logging through SLF4J, so swapping in Log4j2 is a supported and common setup. Teams usually make the switch for asynchronous logging, richer appender options, or better control over structured output.
The key is to change both the dependencies and the configuration file. If you only add Log4j2 without removing the default starter, you often end up with multiple logging implementations on the classpath.
Replace Boot's Default Logging Starter
For Maven, exclude spring-boot-starter-logging from the starter you already use and add spring-boot-starter-log4j2:
The Gradle equivalent is the same idea:
After changing dependencies, run a clean build and inspect the dependency tree if logging behaves strangely.
Use log4j2-spring.xml, Not Just log4j2.xml
If your application is a Spring Boot app, put the configuration in src/main/resources/log4j2-spring.xml. The -spring variant lets Boot participate in initialization and enables useful features such as Spring-profile sections.
Here is a practical starting point:
This gives you readable console output in development and a rolling file for longer-lived environments.
Keep Application Code on the Logging Facade
Even after switching implementations, your application code normally stays on the SLF4J API. That keeps your code portable and avoids hard-coding a specific backend everywhere.
That code works because Boot wires the SLF4J calls to the Log4j2 backend you configured.
Profile-Specific Logging
One reason to prefer log4j2-spring.xml is Spring-aware sections. Development often wants console-heavy debug output, while production wants cleaner info-level logs and maybe file or JSON appenders.
This keeps environment-specific behavior in one place instead of scattering logging changes across multiple files.
When Async Logging Helps
Log4j2 supports asynchronous appenders and asynchronous loggers. That can reduce request-thread overhead in high-throughput services, but it is not a free performance switch. Async logging changes queueing behavior and failure characteristics, so you should measure it under load rather than enabling it blindly.
A common JVM property for async loggers is:
Use it when log volume is large enough to matter and when the team understands the operational tradeoffs.
Common Pitfalls
The most common mistake is leaving Logback on the classpath. If both backends remain present, startup warnings and confusing behavior follow quickly.
Another mistake is putting Spring-specific sections into log4j2.xml and expecting them to work. Use log4j2-spring.xml when you need Spring profile support.
Teams also overuse root-level DEBUG in production. That generates noise, increases I/O, and often hides the messages that actually matter during incidents.
Finally, do not switch to Log4j2 and then call the backend directly from application code unless you truly need backend-specific features. Most applications should keep logging through SLF4J.
Summary
- Replace Boot's default logging starter with
spring-boot-starter-log4j2. - Put the configuration in
log4j2-spring.xmlso Spring-specific features work. - Keep application code on the SLF4J API.
- Use rolling appenders and targeted package loggers instead of global debug logging.
- Treat async logging as a measured optimization, not a default checkbox.
Related reading
- Spring Actuator + Kafka Streams - Add kafka stream status to health check endpoint
- Spring actuator's liveness and readiness are returning 404
- Spring and scheduled tasks on multiple instances
- Spring Boot - Different systems eureka , zuul, ribbon, nginx, used for what?
- Spring-Boot MVC Template Not Loading 404 Not Found
- Spring-boot required a bean named 'entityManagerFactory' that could not be found
- Spring Boot - How to log all requests and responses with exceptions in single place?
- Spring Boot - no log file written logging.file is not respected

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.