Brief introduction:
Spring boot all internal logs use Apache's commons logging component, while also opening the underlying log implementation.
Spring Boot provides default configuration for 3 log components Java Util logging,log4j2,logback, and console output is preset for each, and file output is available as an option.
If you use the Spring Boot starters component, the Logback component is used by default.
Spring boot provides the appropriate Logback routing to ensure that the dependent libraries work properly when using Java Util Logging, Commons Logging, log4j, or slf4j.
1. Log format
The default log format is as follows
2014-03-05 10:57:51.112 INFO 45469---[ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine:apache tomcat/7.0.522014-03-05 10:57:51.253 INFO 45469---[ost-startstop-1] o.a.c.c.c.[tomcat].[ localhost]. [/] : Initializing Spring embedded Webapplicationcontext2014-03-05 10:57:51.253 INFO 45469---[ost-startstop-1] o.s.web.context.contextloader : Root webapplicationcontext:initialization Completed in 1358 Ms2014-03-05 10:57:51.698 INFO 45469---[ost-startstop-1] O.s.b.c.e.servletregistrationbean : Mapping servlet: ' Dispatcherservlet ' to [/]2014-03-05 10:57:51.702 INFO 45469---[ost-startstop-1] o.s.b.c.embedded.filterregistrationbean : Mapping filter: ' Hiddenhttpmethodfilter ' to: [/*]
The content includes:
Date and time
Log level: ERROR, WARN, INFO, DEBUG, or TRACE.
Process ID
---: delimiter
Thread name: enclosed in square brackets
Logger name: Usually the source class
Log information
2. Console output
The default log configuration passes messages back to the console when the message is written.
By default, messages at the error level, warn level, and info level are logged. You can also enable debug mode by starting the application with the--DEBUG flag.
Run Time:
$ Java-jar Myapp.jar--debug
You can also set the Application.properties file in the
Debug=True
When debug mode is enabled, it means that some internal logger (such as the embedded container, hibernate, and spring Boot) also outputs the debug log.
Whether to start debug mode does not affect whether user-defined debug logs are displayed.
Similarly, some internal logger in the system can output trace information in the form of--trace or trace=true.
Control color Output
If the terminal supports ANSI, you can improve readability by outputting text with color.
Add settings
#输出彩色日志spring. output.ansi.enabled=always
[This part has not been closely scanned]
You can set to spring.output.ansi.enabled
a supported value to override the auto detection.
Color coding is configured by using the %clr
conversion Word. In it 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 mapping of log levels to colors:
| Level
Color |
fatal |
Red |
error |
Red |
warn |
Yellow |
Code class= "literal" >info |
Green |
debug |
Green |
trace |
Green |
Alternatively, can specify the color or style that should is used by providing it as a option to the conversion. For example, the text yellow, and use the following setting:
%CLR (%d{yyyy-mm-dd HH:mm:ss. SSS}) {YELLOW}
The following colors and styles are supported:
blue
cyan
faint
green
magenta
red
yellow
3. File output
By default, Spring boot will only output log information in the console. We can set the spring boot to output the log information to a file.
Can be set in the Application.properties file logging.file
orlogging.path属性
- When none of the 2 properties are set, only the log is output in the console
- Logging.file=my.log: Written to a specific file, can be a relative path or an absolute path
- Logging.path=/var/log: Write to a specific folder, which can be a relative path or an absolute path
By default, when a file reaches 10MB, a file is re-created to continue recording, default record, ERROR
WARN
INFO等级的日志。
- Setting logging.file.max-size can change the file size limit
- By default, the previous log file is saved, and you can set how much of the history log is saved by using the Logging.file.max-history property
Note 2 points:
- The logging system was initialized very early in the application life cycle. Therefore, you cannot set the log properties in the properties file through the @propertysource comment.
- Spring Boot's log properties are independent of the underlying components (such as Logback), so the properties of the component (such as Logback's Logback.configurationfile) are not managed by spring boot, and spring Boot only manages its own log properties (properties of the upper limit discussion)
4. Log level
You can set the logging level for all supported log systems in a spring environment, such as application.properties.
By logging.level.<logger-name>=<level>
setting, where Logger-name is the name passed in when calling Logger-name (see below for an example), level is the log rank
Level log levels include: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF
logging.level.root=<level>
Unified configuration can be done by
A usage instance
logging.level.root=WARNlogging.level.org.springframework.web=DEBUGlogging.level.org.hibernate =error
5. Custom Log Configuration
Various log systems can be customized in the following ways:
- Place the appropriate dependent classes under the Classpath
- Then create a corresponding configuration file at the root of the classpath, or use the Spring environment Properties logging.config to indicate the corresponding configuration file
You can force spring boot to use a specific log system through the Org.springframework.boot.logging.LoggingSystem property;
This value must be a fully qualified class name implemented by a log system;
It can also be set to none, which completely disables the spring boot log configuration.
Attention! Because the log system was created before ApplicationContext, the log property can only be set through the properties file, not through the sprint@Configuration文件中使用@PropertySources设置。
Load the corresponding configuration file according to the specified system
Logback:logback-spring.xml, Logback-spring.groovy, Logback.xml, or Logback.groovy
Log4j2:log4j2-spring.xml or Log4j2.xml
JDK (Java Util Logging): logging.properties
We recommend using the-spring naming method
When using Java Util logging, if you run an external executable jar, a class loading problem is generated, so it is recommended to disable the log system when using an external executable jar.
To achieve customization, spring boot transfers some configurations from the spring environment to the system properties
Spring Environment |
System | Property
Comments |
logging.exception-conversion-word
|
LOG_EXCEPTION_CONVERSION_WORD
|
The conversion word used when logging exceptions. |
logging.file
|
LOG_FILE
|
If defined, it is used in the default log configuration. |
logging.file.max-size
|
LOG_FILE_MAX_SIZE
|
Maximum log file size (if Log_file enabled). (Only supported with the default Logback setup.) |
logging.file.max-history
|
LOG_FILE_MAX_HISTORY
|
Maximum number of archive log files to keep (if Log_file enabled). (Only supported with the default Logback setup.) |
logging.path
|
LOG_PATH
|
If defined, it is used in the default log configuration. |
logging.pattern.console
|
CONSOLE_LOG_PATTERN
|
The log pattern to is on the console (stdout). (Only supported with the default Logback setup.) |
logging.pattern.dateformat
|
LOG_DATEFORMAT_PATTERN
|
Appender pattern for Log date format. (Only supported with the default Logback setup.) |
logging.pattern.file
|
FILE_LOG_PATTERN
|
The log pattern to use with a file (if is LOG_FILE enabled). (Only supported with the default Logback setup.) |
logging.pattern.level
|
LOG_LEVEL_PATTERN
|
The format to use is rendering the log level (default %5p ). (Only supported with the default Logback setup.) |
PID
|
PID
|
The current process ID (discovered if possible and when is not already defined as an OS environment variable). |
If you want to use placeholders in logging properties, you should use spring boot syntax instead of the underlying framework's syntax. It is worth noting that if you use Logback, you should use: as the delimiter between the property name and the default value, instead of using:-.
[This paragraph is not quite clear]
You can add MDC and other Ad-hoc content to log lines by overriding-the LOG_LEVEL_PATTERN
(or with logging.pattern.level
logback). For example, if, and then the logging.pattern.level=user:%X{user} %5p
default log format contains a MDC entry for "user", if it exists, as shown in the following example.
2015-09-30 12:30:04.031 user:someone INFO 22174---[ nio-8080-exec-0] Demo. Controllerhandling Authenticated request
Fdsa
6. logback Extension
In this section, you need to see the spring boot official documentation yourself
7. Practical Application
"Spring Boot Official Document" 26, log logs