Logging Configuration in Spring Boot
Fast recall
Quick revision
Core answer
logging.level.*, logging.file.name, and groups belong in application properties or YAML. For Logback-specific appenders, rolling policy, encoders, or profiles, use logback-spring.xml; this applies only while Logback is the selected provider.Interview-ready guide
Interview answer
In Spring Boot, logging is configured on two levels, and which one you use depends on how much control you need.
By default Spring Boot ships with SLF4J as the API and Logback as the implementation, so you usually don't add any logging dependency at all. yml. root equals INFO as a baseline, then raise your own packages to DEBUG.
console. That handles the majority of what most apps need. When you need full control — things like rolling log files that archive daily and keep thirty days of history, multiple appenders, or JSON output for log aggregation — you add a logback configuration file.
xml. The spring-aware version lets Logback use Spring features like profiles, so you can say use a JSON file appender only in the prod profile, and console only in dev. Inside that XML you define appenders, where each appender is a destination — console, a rolling file, or a remote sink — and you attach them to loggers with specific levels.
A common mistake is using a plain file appender with no rolling policy, because then the log file just grows until it fills the disk. xml when you need rolling files, multiple destinations, or profile-specific behavior.
Logging Configuration in Spring Boot
Direct Answer
When a Spring Boot application uses its default logging starter, SLF4J events are handled by Logback. Common settings such as logging.level.*, logging.file.name, and groups belong in application properties or YAML. For Logback-specific appenders, rolling policy, encoders, or profiles, use logback-spring.xml; this applies only while Logback is the selected provider.
Deep Dive & Architecture
Spring Boot starters normally bring spring-boot-starter-logging, which routes SLF4J to Logback. That default can be excluded and replaced, so configuration should match the provider actually on the classpath.
Provider-independent Boot settings handle common needs: logging.level.com.example.orders=DEBUG, logging.level.root=INFO, and logging.file.name=logs/app.log. Environment-specific values can come from normal Spring profiles and configuration sources.
Use logback-spring.xml when Logback features are needed, such as several appenders, a rolling policy, a structured encoder, or <springProfile> blocks. Prefer the -spring file name when using Boot's extensions because a plain logback.xml is loaded too early for some Spring features.
Code Example
# Levels
logging.level.root=INFO
logging.level.com.bookstore=DEBUG
logging.level.org.springframework.web=WARN
# Write to a file (rolls daily/size by default)
logging.file.name=logs/app.log
# Custom console + file patterns
logging.pattern.console=%d{HH:mm:ss} %-5level [%X{requestId}] %logger{36} - %msg%n
Common Mistakes & Pitfalls
- Naming it logback.xml when using Spring features: use logback-spring.xml so
<springProfile>and property substitution work. - No rolling policy: a plain file appender grows unbounded until the disk fills.
- Setting root to DEBUG: floods logs with framework internals; raise only your own packages.
Check your understanding
Answer these without looking back at the article.
- What is SLF4J and why is it used instead of logging directly with a framework?
- How do you create and use a Logger in Java with SLF4J?
- How do you add SLF4J logging to a Java class and write your first log statement?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.