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:
2018-03-13 11:05:23.821 INFO 12092 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9091 (http)
- 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
withWARN
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
settingspring.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 configurelogging.file
orlogging.path
properties in .
logging.file
, the setting file, which 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
: Log level control prefix, * is the package name or Logger nameLEVEL
:选项TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFFExample:
logging.level.com.didispace=DEBUG:com.didispace
All classes under the package are output at DEBUG levellogging.level.root=WARN:root
Logs are output at WARN levelCustom 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 uselogback-spring.xml
it insteadlogback.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 style of output to file (JDK Logger is not supported)
Spring Boot log management
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325333702&siteId=291194637
Recommended
Ranking