Mastering Spring Boot Logging: Formats, Levels, and Custom Configurations
This guide explains Spring Boot 2.4.12 logging fundamentals, covering default console output, customizable patterns, colored logs, file rotation, log levels, logger groups, and advanced Logback extensions, with practical YAML and XML examples for fine‑tuning your application's logging behavior.
Logging Configuration Explained
Spring Boot uses Commons Logging to record internal logs while keeping the underlying logging implementation active. Default configurations are provided for JUL, Log4j2, and Logback, with console output enabled and optional file output.
When using starters, Logback is the default logger, and appropriate routing ensures that libraries using JUL, Commons Logging, Log4j, or SLF4J work correctly.
Java has many logging frameworks; usually you do not need to change the defaults.
1.1 Log Format
Spring Boot’s default log output looks like the following example:
<code>2019-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]</code>The output includes:
Date and time with millisecond precision.
Log level (ERROR, WARN, INFO, DEBUG, TRACE).
Process ID.
Separator (---) before the actual message.
Thread name (in brackets).
Logger name (usually the source class).
The log message.
1.2 Console Output
By default, messages are echoed to the console. ERROR, WARN, and INFO levels are logged. Adding the --debug flag when starting the application enables debug mode.
<code>$ java -jar myapp.jar --debug</code>You can also set debug=true in application.properties .
Debug mode configures a set of core loggers (embedded container, Hibernate, Spring Boot) to emit more information, but it does not set the root logger to DEBUG. Using --trace or trace=true enables trace mode for those core loggers.
1.3 Colored Output
If the terminal supports ANSI, Spring Boot can colorize output for readability. The property spring.output.ansi.enabled can override automatic detection.
Use the %clr conversion word to apply colors based on log level:
<code>%clr(%5p)</code>Level‑to‑color mapping:
FATAL – red
ERROR – red
WARN – yellow
INFO – green
DEBUG – green
TRACE – green
You can also specify a color directly, e.g.:
<code>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}</code>Supported colors and styles: blue, cyan, faint, green, magenta, red, yellow.
Configuration can be placed in logback.xml , logback-spring.xml , or application.properties :
<code>logging:
pattern:
console: '%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){Green} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}'</code>1.4 Log File Output
By default, Spring Boot logs only to the console. To write to a file, set logging.file.name or logging.file.path in application.properties . The table below shows the effect of each property:
(none) – logs only to console.
Specific file – writes to the given file (e.g., my.log ).
Specific directory – writes spring.log to the specified directory (e.g., /var/log ).
Log files rotate at 10 MB and, like console output, record ERROR, WARN, and INFO levels by default.
Note: logging.file.name and logging.file.path cannot be used together.
<code>logging:
level:
com.pack: info
web: trace
file:
name: d:/logs/l.log</code>1.5 Log Rotation / Rolling
When using Logback, you can fine‑tune rolling settings via application.properties or application.yaml . For other systems (e.g., Log4j2) you must configure the rolling policy directly.
Supported rolling properties include:
logging.logback.rollingpolicy.file-name-pattern – pattern for archived file names.
logging.logback.rollingpolicy.clean-history-on-start – whether to clean archives on startup.
logging.logback.rollingpolicy.max-file-size – maximum size before rolling.
logging.logback.rollingpolicy.total-size-cap – total size cap for retained archives.
logging.logback.rollingpolicy.max-history – maximum number of archived files (default 7).
<code>logging:
logback:
rollingpolicy:
max-file-size: 2KB</code>1.6 Log Levels
All supported logging systems can set levels using logging.level. properties (e.g., logging.level.root=warn , logging.level.org.springframework.web=debug ). Levels include TRACE, DEBUG, INFO, WARN, ERROR, FATAL, and OFF.
<code>logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"
com.pack: "error"</code>Environment variables can also set levels, e.g., LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG .
1.7 Logger Groups
Grouping related loggers simplifies bulk configuration. For example, you can define a tomcat group and set its level in one place:
<code>logging:
group:
tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
logging:
level:
tomcat: "trace"</code>Spring Boot provides predefined groups such as web (covers core Spring MVC packages) and sql (covers JDBC and Hibernate SQL loggers).
1.8 Custom Log Configuration
You can activate a specific logging system by including the appropriate library on the classpath and pointing logging.config to a configuration file (e.g., logback-spring.xml , log4j2-spring.xml , or logging.properties ).
The property org.springframework.boot.logging.LoggingSystem can force Spring Boot to use a particular implementation, or none to disable Spring Boot’s logging configuration entirely.
Logging is initialized before the ApplicationContext , so it cannot be controlled via @PropertySource in configuration classes; system properties are the only way to modify or disable it.
Relevant Spring environment properties are mapped to system properties for use in configuration files (e.g., LOG_EXCEPTION_CONVERSION_WORD , LOG_FILE , CONSOLE_LOG_PATTERN , FILE_LOG_PATTERN , etc.).
<code><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[${PID}] - [${LOG_FILE}] - [${LOG_LEVEL_PATTERN}] %yellow(%date{yyyy-MM-dd HH:mm:ss}) |%highlight(%-6level) |%green(%logger:%line) |%black(%msg%n)</pattern>
<charset>UTF-8</charset>
</encoder>
</appender></code>1.8.1 Profile‑Specific Configuration
The <springProfile> tag allows inclusion or exclusion of configuration sections based on the active Spring profile. Example:
<code><springProfile name="staging">
<!-- configuration for staging -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration for dev or staging -->
</springProfile>
<springProfile name="!production">
<!-- configuration when production is not active -->
</springProfile></code>1.8.2 Spring Property Integration
The <springProperty> tag exposes Spring environment properties to Logback, similar to Logback’s <property> but sourcing values from the environment.
<code><springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
</appender></code>Example of using a custom property:
<code>custom:
props:
host: 127.0.0.1</code> <code><springProperty scope="context" name="host" source="custom.props.host" defaultValue="localhost"/></code> <code><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[${host}] - [${PID}] - [${LOG_FILE}] - [${LOG_LEVEL_PATTERN}] %yellow(%date{yyyy-MM-dd HH:mm:ss}) |%highlight(%-6level) |%green(%logger:%line) |%black(%msg%n)</pattern>
<charset>UTF-8</charset>
</encoder>
</appender></code>Test output example:
<code>[127.0.0.1] - [23188] - [d:/logs/l.log] - [INFO] 2021-09-23 14:46:46 | INFO</code>Done!!
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.