Master Spring Boot Logging: Configuration, Color, Files, and Rotation
This guide explains how Spring Boot 3.2.5 configures logging with Logback, Java Util Logging, Log4j2 and Commons Logging, covering default formats, color output, file writing, rotation policies, log levels, grouping, shutdown hooks, custom configuration files, and Logback extensions.
Environment: Spring Boot 3.2.5
1. Introduction
Spring Boot uses Commons Logging for internal logging and provides default configurations for Java Util Logging, Log4j2 and Logback. By default, Logback is used when starters are present, and it routes other logging APIs so they work correctly.
Note: If the list of Java logging frameworks looks confusing, you usually only need to change the logging dependency and Spring Boot defaults will work.
2. Detailed Configuration
2.1 Log format
Default log output looks like the following example:
Explanation of each field:
Date and time with millisecond precision.
Log level: ERROR, WARN, INFO, DEBUG or TRACE.
Process ID.
Separator "---" to mark the start of the actual message.
Application name in brackets (only when spring.application.name is set).
Thread name in brackets.
Logger name, usually the source class.
Log message.
Note: Logback does not have a FATAL level; it is mapped to ERROR.
To disable application name in logs:
<code>logging:
include-application-name: false
</code>Enable debug mode:
<code>debug: true
</code>Or via command line:
<code>java -jar app.jar --debug
</code>Trace level can also be enabled:
<code>trace: true
</code>2.2 Colored output
If the terminal supports ANSI, logs can be colored. Configure with:
<code>spring:
output:
ansi:
enabled: always
</code>Values: DETECT, ALWAYS, NEVER.
Color codes are configured with %clr conversion word, e.g.:
<code>%clr(%5p)
</code>Mapping of levels to colors is shown in the table.
To change colors:
<code>logging:
pattern:
console: "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss}}){yellow} %clr(${LOG_LEVEL_PATTERN:-%5p}) ..."
</code>2.3 File output
By default, Spring Boot logs only to console. To write to a file set logging.file.name or logging.file.path :
<code>logging:
file:
name: myapp.log
</code>Do not configure both name and path simultaneously; use logging.file.path with the default file name spring.log if you need a custom directory.
Log rotation occurs when the file reaches 10 MB, with default retention of 7 files.
2.4 Log rotation
When using Logback, fine‑tune rotation in application.yml :
<code>logging:
logback:
rollingpolicy:
file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz
clean-history-on-start: false
max-file-size: 1KB
total-size-cap: 0B
max-history: 7
</code>2.5 Log levels
Set levels with logging.level.<logger-name>=<level> , e.g.:
<code>logging:
level:
root: info
web: debug
sql: debug
"[org.springframework.web]": "debug"
"[org.hibernate]": "debug"
</code>2.6 Log grouping
Group related loggers:
<code>logging:
group:
tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
</code>Then set level for the group:
<code>logging:
level:
tomcat: debug
</code>2.7 Shutdown hook
Spring Boot registers a JVM shutdown hook to clean up logging resources. Disable it with:
<code>logging:
register-shutdown-hook: false
</code>2.8 Custom configuration file
Specify a custom Logback configuration file:
<code>logging:
config: logback.xml
</code>2.9 Logback extensions
Use logback-spring.xml to access Spring properties and profiles, e.g.:
<code><springProfile name="prod">
<root level="ERROR">
<appender-ref ref="FILEERROR" />
</root>
</springProfile>
<springProfile name="dev">
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</springProfile>
</code>Expose Spring environment properties in Logback with <springProperty> :
<code><springProperty scope="context" name="appHost" source="pack.host" defaultValue="localhost"/>
</code>Then use the property in a pattern:
<code><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm} - ${appHost} - %-5level %logger Line:%-3L - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
</code>Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.