Spring Boot uses Commons Logging for all internal logs , but the default configuration also provides support for common logs such as: Java Util Logging , Log4J , Log4J2 and Logback . Each Logger can be configured to use console or file output log content.
format log
The default log output is as follows:
2016-04-13 08:23:50.120 INFO 37397 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {4.3.11.Final}
The output content elements are as follows:
- time date - accurate to milliseconds
- Log level — ERROR, WARN, INFO, DEBUG or TRACE
- Process ID
- delimiter -
---
identifies the start of the actual log - Thread name - in square brackets (may truncate console output)
- Logger name — usually the class name of the source code
- log content
console output
By default, Spring Boot configures the log output to the console ERROR
with WARN
and level.INFO
We can switch to DEBUG
levels in two ways:
--debug
Add the flag after running the command , like:$ java -jar myapp.jar --debug
- In
application.properties
the configurationdebug=true
, when this property is set to true, the core Logger (including embedded container, hibernate, spring) will output more content, but the log of your own application will not be output as DEBUG level.
colorful output
If your terminal supports ANSI, setting colored output will make the log more readable. Supported by application.properties
setting spring.output.ansi.enabled
parameters in .
- NEVER: Disable ANSI-colored output (default)
- DETECT: Will check whether the terminal supports ANSI, and if so, use color output (recommended)
- ALWAYS: Always use ANSI-colored format for output. If the terminal does not support it, there will be a lot of interference information, which is not recommended.
file output
The default configuration of Spring Boot will only be output to the console and will not be recorded in a file, but we usually need to record it in a file when using it in a production environment.
To increase file output, you need application.properties
to configure logging.file
or logging.path
properties in .
- logging.file, the setting file, can be an absolute path or a relative path. Such as:
logging.file=my.log
- logging.path, set the directory, the spring.log file will be created in this directory, and the log content will be written, such as:
logging.path=/var/log
The log file will be truncated when the size is 10Mb, and a new log file will be generated. The default level is: ERROR, WARN, INFO
level control
In Spring Boot, you only need application.properties
to configure the level control of logging to complete.
Configuration format:logging.level.*=LEVEL
logging.level
: The log level control prefix, which*
is the package name or Logger nameLEVEL
:选项TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
Example:
logging.level.com.didispace=DEBUG
:com.didispace
All classes under the package are output at DEBUG levellogging.level.root=WARN
: root log output at WARN level
Custom log configuration
Since the logging service is generally initialized before the ApplicationContext is created, it does not have to be controlled by Spring's configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.
Depending on the logging system, you can organize the configuration file names according to the following rules, and they will be loaded correctly:
- Logback:
logback-spring.xml
,logback-spring.groovy
,logback.xml
,logback.groovy
- Log4j:
log4j-spring.properties
,log4j-spring.xml
, ,log4j.properties
,log4j.xml
- Log4j2:
log4j2-spring.xml
,log4j2.xml
- JDK (Java Util Logging):
logging.properties
Spring Boot officially recommends to use -spring
the file name with it as your log configuration (if you use logback-spring.xml
it instead logback.xml
)
Custom output format
In Spring Boot, the application.properties
output format can be controlled by configuring the following parameters:
- logging.pattern.console: defines the style of output to the console (JDK Logger is not supported)
- logging.pattern.file: defines the pattern of output to file (JDK Logger is not supported)