Spring Boot auto-configures Logback as the default logging framework whenDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
spring-boot-starter-logging is on the classpath (pulled in transitively by most starters). Most logging changes can be made through application.properties or application.yaml without writing any XML — but when you need fine-grained control, Spring Boot provides clean extension points for custom Logback and Log4j2 configuration.
Set log levels
To control verbosity at runtime, set log levels using thelogging.level prefix followed by the logger name:
- application.yaml
- application.properties
logging.level.root sets the global fallback level. Any package or class name can be used as the key. Valid levels are trace, debug, info, warn, error, fatal, and off.
Write logs to a file
By default, Spring Boot logs only to the console. To also write to a file, setlogging.file.name:
- application.yaml
- application.properties
spring.log inside it), use logging.file.path instead:
- application.yaml
- application.properties
logging.logback.rollingpolicy.max-file-size to adjust this.
Customize the log format
To change the console log pattern:- application.yaml
- application.properties
Enable color output in the console
Spring Boot uses ANSI color output on terminals that support it. Color is enabled by default when a supported terminal is detected. To force-enable or force-disable it:- application.yaml
- application.properties
spring.output.ansi.enabled are always, detect (default), and never.
Use a custom logback-spring.xml
To apply Logback settings beyond whatapplication.properties supports, add a logback-spring.xml to the root of your classpath (src/main/resources/logback-spring.xml). Using the -spring suffix lets you use Spring Boot’s Logback extensions (profile-based configuration, environment property injection).
A typical custom configuration that reuses Spring Boot’s default patterns:
org/springframework/boot/logging/logback/:
| File | Purpose |
|---|---|
defaults.xml | Conversion rules, pattern properties, common logger configs |
console-appender.xml | Console appender using CONSOLE_LOG_PATTERN |
file-appender.xml | Rolling file appender using FILE_LOG_PATTERN |
structured-console-appender.xml | Console appender with structured logging |
structured-file-appender.xml | File appender with structured logging |
Configure file-only output
To disable console logging and write only to a file, importfile-appender.xml but not console-appender.xml:
application.properties or application.yaml:
| Property | Description |
|---|---|
${PID} | Current process ID |
${LOG_FILE} | Value of logging.file.name |
${LOG_PATH} | Value of logging.file.path |
${LOG_EXCEPTION_CONVERSION_WORD} | Value of logging.exception-conversion-word |
${ROLLING_FILE_NAME_PATTERN} | Value of logging.pattern.rolling-file-name |
Switch from Logback to Log4j2
To replace Logback with Log4j2, excludespring-boot-starter-logging and add spring-boot-starter-log4j2.
- Maven
- Gradle
To ensure that
java.util.logging output is routed through Log4j2, set the system property java.util.logging.manager=org.apache.logging.log4j.jul.LogManager at JVM startup.Use YAML or JSON configuration with Log4j2
Log4j2 supports YAML and JSON configuration files in addition to its default XML format. Add the required dependencies and name your file accordingly:| Format | Additional dependency | File name |
|---|---|---|
| YAML | jackson-databind + jackson-dataformat-yaml | log4j2.yaml or log4j2.yml |
| JSON | jackson-databind | log4j2.json or log4j2.jsn |
Combine multiple Log4j2 configuration files
To merge a secondary configuration file on top of a primary one (useful for overriding specific loggers per environment), setlogging.log4j2.config.override:
optional: prefix prevents the application from failing to start if the override file is absent.