Mastering Spring Boot Logging: Formats, Colors, Files, and Custom Configurations
This guide explains how Spring Boot’s default logging uses Logback with Commons Logging, details the console log format, color coding, file output options, rolling policies, log levels, logger groups, and advanced customizations through logback‑spring.xml and environment properties.
1.1 Log Format
Spring Boot’s default console 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</code> <code>2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext</code> <code>2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms</code> <code>2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]</code> <code>2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]输出以下项目:</code>Date and time with millisecond precision (easy to sort)
Log level (ERROR, WARN, INFO, DEBUG, TRACE)
Process ID
--- separator before the actual message
Thread name in brackets (may be truncated in console)
Logger name, usually the source class
The log message itself
1.2 Console Output
By default, Spring Boot writes logs to the console, showing messages at ERROR, WARN, and INFO levels. Adding the --debug flag when launching the application enables debug mode, which configures a set of core loggers to emit more detailed information.
<code>$ java -jar myapp.jar --debug</code>You can also enable debug mode by setting debug=true in application.properties . The --trace flag (or trace=true ) provides an even more verbose trace logging for core components.
1.3 Colored Console Output
If the terminal supports ANSI, Spring Boot can color‑code log lines to improve readability. The spring.output.ansi.enabled property can override automatic detection.
Use the %clr conversion word to apply colors based on log level:
<code>%clr(%5p)</code>Color mapping:
FATAL – Red
ERROR – Red
WARN – Yellow
INFO – Green
DEBUG – Green
TRACE – Green
You can also specify a color or style directly, e.g.:
<code>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}</code>Supported colors and styles include blue , cyan , faint , green , magenta , red , and yellow . These can be configured in logback.xml , logback-spring.xml , or via application.properties :
<code>logging:</code><code> pattern:</code><code> 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 File Output
By default, Spring Boot logs only to the console. To also write to a file, set either logging.file.name (specific file) or logging.file.path (directory) in application.properties . Example configurations:
logging.file.name=my.log – writes to the specified file.
logging.file.path=/var/log – writes to spring.log inside the given directory.
Log files rotate at 10 MB and, like console output, record ERROR, WARN, and INFO levels by default. Note that logging.file.name and logging.file.path cannot be used together.
<code>logging:</code><code> level:</code><code> com.pack: info</code><code> web: trace</code><code> file:</code><code> name: d:/logs/l.log</code>1.5 Rolling Policies
When using Logback, you can fine‑tune rolling settings via application.properties or application.yaml . For other systems (e.g., Log4j2) you must configure rolling directly in their respective XML files.
logging.logback.rollingpolicy.file-name-pattern – pattern for archived file names.
logging.logback.rollingpolicy.clean-history-on-start – whether to clean old 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:</code><code> logback:</code><code> rollingpolicy:</code><code> max-file-size: 2KB</code>This limits each log file to 2 KB; larger files are automatically archived and compressed.
1.6 Log Levels
All supported logging systems allow setting levels with the logging.level.<logger name>=<level> property (e.g., in application.properties ). Levels include TRACE, DEBUG, INFO, WARN, ERROR, FATAL, and OFF. The root logger can be configured via logging.level.root .
<code>logging:</code><code> level:</code><code> root: "warn"</code><code> org.springframework.web: "debug"</code><code> org.hibernate: "error"</code><code> com.pack: "error"</code>Environment variables can also set levels, e.g., LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG .
1.7 Logger Groups
Spring Boot lets you define logger groups to change several loggers at once. Example group definition in application.properties :
<code>logging:</code><code> group:</code><code> tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"</code>After defining the group, you can set its level with a single entry:
<code>logging:</code><code> level:</code><code> tomcat: "trace"</code>Pre‑defined groups include web (various Spring MVC packages) and sql (JDBC and Hibernate SQL loggers).
1.8 Custom Log Configuration
You can activate a specific logging system by adding the appropriate library to the classpath and specifying the configuration file location with the logging.config property.
To force Spring Boot to use a particular logging system, set org.springframework.boot.logging.LoggingSystem to the fully qualified class name of the desired LoggingSystem implementation, or use none to disable Spring Boot’s logging configuration entirely.
Logging is initialized before the ApplicationContext is created, so it cannot be controlled via @PropertySource in configuration classes; the only way to change or disable the system is through system properties.
1.9 Logback Extensions
Spring Boot provides several Logback extensions that enable advanced configuration in logback-spring.xml . The <springProfile> tag allows conditional inclusion based on active Spring profiles, and <springProperty> exposes Spring environment properties for use inside Logback.
<code><springProfile name="staging"></code><code> <!-- configuration for the "staging" profile --></code><code></springProfile></code> <code><springProperty scope="context" name="host" source="custom.props.host" defaultValue="localhost"/></code>Example of using a custom property in an appender:
<code><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"></code><code> <encoder></code><code> <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></code><code> <charset>UTF-8</charset></code><code> </encoder></code><code></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.