Structured Logging with SLF4J and Logback in Spring Boot
Fast recall
Quick revision
Core answer
Whether you set up production-grade logging beyond basic System.out.println
Using System.out.println or creating Logger inside methods instead of as static final fields
Mention structured JSON logging for log aggregation systems like ELK or Splunk
Interview-ready guide
Interview answer
Spring Boot auto-configures SLF4J and Logback, so the logging framework is available from the first line of your application without any setup. The meaningful decisions are about log format, configuration, and what to log.
SLF4J as the facade
- SLF4J is the logging facade — your code calls
LoggerFactory.getLogger(MyClass.class)and useslogger.info(),logger.error(), etc.; the actual implementation (Logback, Log4j2) is a dependency on the classpath - Which means you can switch from Logback to Log4j2 by swapping a dependency without touching a single logging call in your application code
Logback configuration
- Spring Boot looks for
logback-spring.xmlin the classpath — usinglogback-spring.xmlinstead oflogback.xmlenables Spring Boot'selements for environment-specific configuration - Configure the root log level with
logging.level.root=WARNinapplication.propertiesand override for specific packages:logging.level.com.myapp=DEBUG— which means third-party library noise is suppressed while your application logs at DEBUG
Structured logging — JSON output
- Add
logstash-logback-encoderand configure a JSON encoder inlogback-spring.xml— each log entry becomes a JSON object with standard fields (timestamp,level,logger,message) plus any MDC values - Which means Elasticsearch, CloudWatch Logs Insights, and GCP Logging can filter by field:
level = "ERROR"oruserId = "123"without parsing plain-text log lines
Log correlation with MDC
- MDC (Mapped Diagnostic Context) is a thread-local map —
MDC.put("traceId", traceId)makes the value available in all subsequent log entries from that thread without passing it explicitly - Spring Cloud Sleuth automatically populates
traceIdandspanIdin MDC — which means every log line from a request includes the trace ID, enabling correlation between logs and traces in Jaeger
Structured Logging with SLF4J and Logback in Spring Boot
Direct Answer
Spring Boot includes SLF4J and Logback by default. Configure Logback in logback-spring.xml using structured JSON appenders for production and human-readable patterns for development. Use LoggerFactory.getLogger() in code and configure log levels per package in application.properties.
Common Mistakes & Pitfalls
Using System.out.println or creating Logger inside methods instead of as static final fields
Check your understanding
Answer these without looking back at the article.
- How do you prevent sensitive data like passwords from appearing in logs?
- How does Logback's AsyncAppender improve performance?
- What is the difference between Logback and Log4j2?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.