Spring Boot uses Commons Logging for comprehensive logging internally, but leaves the underlying logging implementation open. Default configurations are provided for Java Util Logging, Log4J2 and Logback. In each case, logging managers are pre-configured to use console output with the option of outputting to a file.
By default, if Starters are used, Logback is used for logging. Appropriate Logback routing is also provided to ensure correct operation of dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J.
Logger Format
The default log output in Spring Boot is similar to the following example:
2022-10-20 12:40:11.311 INFO 16138 --- [ main] o.s.b.d.f.s.MyApplication : Starting MyApplication using Java 1.8.0_345 on myhost with PID 16138 (/opt/apps/myapp.jar started by myuser in /opt/apps/) 2022-10-20 12:40:11.330 INFO 16138 --- [ main] o.s.b.d.f.s.MyApplication : No active profile set, falling back to 1 default profile: "default" 2022-10-20 12:40:13.056 INFO 16138 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-10-20 12:40:13.070 INFO 16138 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-10-20 12:40:13.070 INFO 16138 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.68] 2022-10-20 12:40:13.178 INFO 16138 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-10-20 12:40:13.178 INFO 16138 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1762 ms 2022-10-20 12:40:13.840 INFO 16138 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-10-20 12:40:13.850 INFO 16138 --- [ main] o.s.b.d.f.s.MyApplication : Started
The following elements are output:
Date and time: millisecond accuracy and easy sorting.
Log level:
ERROR
,WARN
,INFO
,DEBUG
orTRACE
.Process ID.
Separator
---
to separate the beginning of the actual log messages.Stream name: enclosed in square brackets (can be truncated for console output).
Log manager name: usually the name of the original class (often shortened).
Log message .
FATAL
level. It is displayed as ERROR
.
Console output
In the default log configuration, messages are reflected to the console as they are
written. By default, messages at the ERROR
, WARN
and INFO
levels are written
to the log. You can also enable "debug" mode by running the application with the --debug
flag.
$ java -jar myapp.jar --debug
debug=true
in the
application.properties
file.
If debug mode is enabled, a set of core logging managers (built-in container, Hibernate and Spring Boot) are
configured to output additional information. Enabling debug mode
does configure the application to log all messages at level DEBUG
.
You can also enable
"trace" mode by running the application with the --trace
(or trace=true
in the
application.properties
file). This allows you to log trace logs for a number of major logging managers
(the built-in container, Hibernate schema generation, and the entire Spring portfolio).
Color-highlighted output
If the terminal supports ANSI, this is used to make it easier to read color output. You can install
spring.output.ansi.enabled
in supported value to override automatic detection.
Color encoding is configured
using the conversion word %clr
. In its simplest form, the converter colors the output according to the
log level, as shown in the following example:
%clr(%5p)
The following table describes the correspondence between log levels and colors:
Level | Color |
---|---|
|
Red |
|
Red |
|
Yellow |
|
Green |
|
Green |
|
Green |
You can also specify the color or style you want to use , specifying it as a parameter for the conversion. For example, to make the text yellow, use the following configuration:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
The following colors and styles are supported:
blue
cyan
faint
green
magenta
red
yellow
Output to a file
By default, Spring Boot logs only in the console and does not write to log files. If
you need to write to log files in addition to output to the console, you must set the logging.file.name
or logging.file.path
property (for example, in the file application.properties
).
The following table shows how the logging.*
properties can be used together:5
logging.file.name |
logging.file.path |
Example | Description |
---|---|---|---|
(absent) |
(absent) |
Displays log entries exclusively to the console. |
|
Specific file |
(absent) |
|
Writes to the specified log file. The names can be an exact location or be associated with the current directory. |
(missing) |
Specific directory |
|
Writes |
Log files are rotated if they reach 10 MB in size, and, like in the case of console output, messages at the
ERROR
, WARN
and INFO
levels are logged by default.
logback.configurationFile
for Logback) are not controlled by Spring Boot.
File rotation
If you are using Logback, you can fine-tune log rotation parameters using the application.properties
or
application.yaml
file. For all other logging systems, you will need to configure the rotation
parameters yourself (for example, if you are using Log4J2, you can add the file log4j2.xml
or log4j2-spring.xml
).
The following rotation policy properties are supported:
Name | Description |
---|---|
|
File name pattern used to create log archives. |
|
If the log archive should be cleaned when the application starts. |
|
The maximum size of a log file before archiving it. |
|
Maximum size of the log archive to be deleted before actual deletion. |
|
Maximum number of files in the log archive that need to be stored (default 7). |
Logging Levels
All supported logging systems can have logging levels set to
Environment
from Spring (for example, application.properties
) using logging.level.<logger-name>=<level>
,
where level
is one of the levels, including TRACE , DEBUG, INFO, WARN, ERROR, FATAL or OFF. You can
configure the root
logging manager using logging.level.root
.
The following example
shows possible logging options in application.properties
:
logging.level.root=warn
logging.level.org.springframework.web=debug
logging .level.org.hibernate=error
logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"
You can also set logging levels using environment variables. For example, LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG
will set org.springframework.web
to DEBUG
.
SPRING_APPLICATION_JSON
variable.
Log Groups
In many cases, it is useful to be able to group related logging managers together so that they could be configured simultaneously. For example, you can frequently change the logging levels for all logging managers associated with Tomcat, but you won't be able to easily remember the top-level packages.
To
help with this, Spring Boot allows you to define groups logging in your Environment
for Spring. For
example, here's how you can define a "tomcat group" by adding it to your application.properties
file:
logging.group.tomcat=org.apache.catalina,org.apache.coyote,org.apache.tomcat
logging:
group:
tomcat:"org.apache.catalina,org.apache.coyote,org.apache.tomcat"
Once defined, you can change the level for all logging managers in a group with one line:
logging.level.tomcat=trace
logging:
level:
tomcat: "trace"
Spring Boot contains the following predefined logging groups that can be used out of the box:
Name | Log Managers |
---|---|
web |
|
sql |
|
Using a log shutdown interceptor
To free up logging resources when an application shuts down, there
is a shutdown interceptor that runs clearing the logging system when exiting the JVM. This completion hook is
registered automatically unless your application is deployed as a war file. If your application has a complex
hierarchy of contexts, a completion interceptor may not meet your needs. If it does not match them, disable the
completion interceptor and consider the options provided directly by the underlying logging system. For example,
Logback provides context
selectors that allow each Logger to be created in its own context. You can use the logging.register-shutdown-hook
property to disable the shutdown hook. Setting the value to false
disables logging. You can set this
property in the application.properties
or application.yaml
file:
logging.register-shutdown-hook=false
logging:
register-shutdown-hook: false
Custom log configuration
Various logging systems can be activated by enabling relevant libraries in
the classpath and further specifically configured by passing the appropriate configuration file in the root of the
classpath or in the location specified by the following Environment
property for Spring: logging.config
.
You can force Spring Boot to use a specific logging system using the org.springframework.boot.logging.LoggingSystem
system property. The value must be the fully qualified name of the
LoggingSystem
implementation class. You can also completely disable the Spring Boot logging
configuration by using the
none
value.
ApplicationContext
, it is not possible to control logging via the @PropertySources
annotation in Spring files with
the @Configuration
annotation. The only way to change the logging system or disable it completely is
through system properties.
Depending on the logging system, the following files are loaded:
Logging system | Settings |
---|---|
Logback |
|
Log4j2 |
|
JDK (Java Util Logging) |
|
-spring
options for logging
configuration (for example, logback-spring.xml
, not logback.xml
). If standard
configuration locations are used, Spring will not be able to fully control log initialization.
To simplify configuration, some other properties are moved from Environment
for Spring to system
properties, as described in the following table :
Spring Environment | System Property | Comments |
---|---|---|
|
|
The conversion word used when logging exceptions. |
|
|
If defined, it is used in the default log configuration. |
|
|
If defined, it is used in the default logging configuration. |
|
|
Log pattern for use with the console (STDOUT). |
|
|
Appender pattern for formatting the log date. |
|
|
Encoding used for logging log with console output. |
|
|
Log pattern to use in the file (if the |
|
|
Encoding used for logging files (if the |
|
|
Format used when visualizing the log level (default |
|
|
Identifier of the current process (recognized if possible and if it is not already defined as an OS environment variable). |
If you use Logback, the following properties are also passed:
Spring Environment | System Property | Comments |
---|---|---|
|
|
Template for names of archived log files (by default |
|
|
Indicates whether files in the log archive should be cleared at startup. |
|
|
Maximum log file size. |
|
|
Total size of stored log backups. |
|
|
The maximum number of files in the log archive that need to be stored. |
All supported logging systems can access system properties when parsing their configuration files.
See examples of default configurations in spring-boot.jar
:
If you need to use a placeholder in a logging property, you should stick to
Spring Boot syntax rather than the underlying framework syntax. In particular, if you use Logback, you should use
:
as the delimiter between the property name and its default value, rather than :-
.
You can add MDC context and other special content to log lines by
overriding only LOG_LEVEL_PATTERN
(or logging. pattern.level
in Logback). For example, if
you use logging.pattern.level=user:%X{user} %5p
, then the default logger format contains an MDC entry
for "user" if it exists, as shown in the following example .
2019-08-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.Controller Handling authenticated request
Logback extensions
Spring Boot contains a number of extensions for Logback that can simplify in-depth
configuration. You can use these extensions in the logback-spring.xml
configuration file.
logback.xml
configuration file is loaded
too early, extensions cannot be used in it. You must either use logback-spring.xml
or define the logging.config
property.
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]] ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current
Profile-specific configuration
The <springProfile>
tag allows you to optionally
include or exclude configuration sections based on active Spring profiles. Profile sections are supported anywhere
in the <configuration>
element. Use the name
attribute to specify which profile
accepts the configuration. The <springProfile>
tag can contain a profile name (for example,
staging
) or a profile expression. A profile expression allows you to express more complex profile logic
(e.g. production & (eu-central | eu-west)
). The following listing shows three sample profiles:
<springProfile name="staging">
<!-- configuration that will be activated if the "staging" profile is active -->
</springProfile>
<springProfile name="dev | staging">
<!-- the configuration that will be activated if the "dev" or "staging" profile is active -->
</springProfile>
<springProfile name="!production">
<!-- configuration that will be activated if the "production" profile is active -->
</springProfile>
Environment Properties
The <springProperty>
tag allows you to open properties from
Environment
in Spring for use in Logback. This can be useful if you need to access values from the
application.properties
file in your Logback configuration. This tag works the same as the standard
<property>
tag in Logback. However, instead of setting a direct value
, you set the
source
property (from Environment
). If you need to store a property outside of the local
accessibility scope, you can use the scope
attribute. If you need a fallback value (in case the
property is not set to Environment
), you can use the defaultValue
attribute. The following
example shows how to expose properties for use in Logback:
<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>
source
in the kebab register (for example,
my.property-name
). However, properties can be added to Environment
using loose rules.
GO TO FULL VERSION