How to set up Spring Boot and log4j2 properly?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot uses Logback as its default logging framework. To switch to Log4j2, you must exclude the spring-boot-starter-logging dependency and add spring-boot-starter-log4j2. Log4j2 offers better performance (async logging with LMAX Disruptor), more flexible configuration (XML, YAML, JSON, properties), and advanced features like garbage-free logging. Configuration goes in log4j2.xml (or log4j2-spring.xml for Spring-aware features) in src/main/resources.
Step 1: Update Dependencies (Maven)
Step 1 (Gradle)
Step 2: Create log4j2-spring.xml
Use log4j2-spring.xml (not log4j2.xml) to enable Spring Boot's profile-aware logging with <SpringProfile> tags.
Step 3: Use SLF4J in Code
Always use SLF4J (org.slf4j.Logger) as the logging facade, not Log4j2's API directly. This keeps your code framework-agnostic.
Async Logging (High Performance)
Async logging with LMAX Disruptor writes log events to a ring buffer instead of directly to appenders, dramatically reducing logging latency in high-throughput applications.
Profile-Specific Configuration
<SpringProfile> tags only work in log4j2-spring.xml, not log4j2.xml.
Common Pitfalls
- Not excluding spring-boot-starter-logging: If Logback remains on the classpath alongside Log4j2, SLF4J cannot determine which binding to use, causing startup warnings or errors. Exclude
spring-boot-starter-loggingfrom all starter dependencies. - Using log4j2.xml instead of log4j2-spring.xml: Spring Boot's
<SpringProfile>and<SpringProperty>tags only work in files namedlog4j2-spring.xml. Plainlog4j2.xmlis loaded before Spring initializes. - additivity="true" causing duplicate log entries: If a logger has
additivity="true"(default), log events propagate to the root logger and get printed twice. Setadditivity="false"on non-root loggers. - Missing jackson-dataformat-yaml for YAML config: If using
log4j2.yamlorlog4j2.ymlformat, the Jackson YAML module must be on the classpath. Without it, Log4j2 silently falls back to default configuration. - Log4j2 security vulnerabilities: The Log4Shell vulnerability (CVE-2021-44228) affected Log4j2 versions before 2.17.0. Always use the latest patched version and disable JNDI lookups if not needed.
Summary
- Exclude
spring-boot-starter-loggingand addspring-boot-starter-log4j2in dependencies - Place configuration in
src/main/resources/log4j2-spring.xmlfor Spring profile support - Use SLF4J (
LoggerFactory.getLogger()) in application code, not Log4j2 API directly - Add LMAX Disruptor +
AsyncLoggerfor high-performance async logging - Set
additivity="false"on non-root loggers to prevent duplicate log entries - Always use the latest Log4j2 version to avoid known security vulnerabilities
Related reading
- How to setup AWS CloudWatch''s agent at Ubuntu to get correct custom metrics like cpu, memory and disk usage
- How to share a file from initContainer to base container in Kubernetes
- How to share rate limiting state between traefik instances?
- How to share variables & logging in Apache Flink?
- How to set UTF-8 character encoding in Spring boot?
- How to setup multiple connection pools when multiple datasources are used in Spring Boot?
- How to show the last queries executed on MySQL?
- How to solve Error loading state AccessDenied Access Denied status code 403 when trying to use s3 for terraform backend?

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.