Logging
Spring Boot uses Logback by default, behind the SLF4J API. Nothing needs adding
to the build — spring-boot-starter-web pulls in
spring-boot-starter-logging transitively.
Default console format
{Date and Time} {Log Level} {Process ID} --- [{Thread name}] {Logger name} : {message}
Pattern conversion words
| Long form | Short | Meaning |
|---|---|---|
%date | %d | Timestamp; %d{HH:mm:ss.SSS} to control the format |
%level | %p | Level; %5p pads to five columns so messages align |
%thread | %t | Thread name |
%logger | %c | Logger name; %logger{39} abbreviates packages to fit |
%message | %m | The log message |
%n | Platform line separator | |
%clr(...) | Spring Boot extension: ANSI colour, optionally {cyan} etc. |
%-40.40logger{39} reads as: left-align, minimum 40 characters, truncate at 40,
and let Logback abbreviate package names to fit within 39.
Custom patterns
# Full default, kept verbatim so it can be edited a piece at a time
logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
# Shorter: time only, narrower logger column
logging.pattern.console=%clr(%d{HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-20.20logger{19}){cyan} %clr(:){faint} %m%n
${LOG_EXCEPTION_CONVERSION_WORD:-%wEx} appends the stack trace when one is
present. Note there is no space before it — a space there prints a stray - on
every line that has no exception, which is the usual reason for that symptom.
Removing the whole placeholder also removes stack traces from the console, so
prefer fixing the spacing.
Writing to a file
# One or the other, not both
logging.file.name=logs/logfile.log # explicit file
logging.file.path=logs # directory; the file is named spring.log
The older logging.file and logging.path keys were deprecated in Spring Boot
2.2 and removed in 2.4. They are silently ignored on current versions, which
looks exactly like logging being misconfigured — check for them first when a log
file fails to appear.
Rotation is on by default once file output is enabled:
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=7
logging.logback.rollingpolicy.total-size-cap=1GB
Without total-size-cap, max-history alone bounds the number of files but not
their combined size. A service that logs heavily can still fill the disk.
Levels
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.com.example.myapp=TRACE
Levels are resolved by longest logger-name prefix, so the more specific entry wins regardless of declaration order.
debug=true in application.properties is not the same thing: it enables the
condition evaluation report and DEBUG for a selection of core loggers, not for
your own code.
Full control
For anything beyond patterns and levels, add logback-spring.xml to
src/main/resources. Prefer the -spring suffix over plain logback.xml:
Spring Boot loads it later in the startup process, which is what makes
<springProfile> and <springProperty> available.
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProfile name="prod">
<root level="INFO"/>
</springProfile>
<springProfile name="dev">
<root level="DEBUG"/>
</springProfile>
</configuration>