SpringBoot document translation series-26. Log logging, springbootlogging
Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/7613854.html
This is the SpringBoot log Content.
26Logs
Spring uses CommonLogging as a built-in logging tool, but it also retains the implementation of underlying logs. The default configuration provides Java Util Logging, Log4J2, and LogBack. In different situations, you can select console output or file output through pre-configuration.
By default, if you use SpringBoot's "Starters", LogBack will be used as a log tool. It has an appropriate rollback route to ensure that the dependent libraries of Java Util Logging, Log4J2, and Commons Logging can work normally.
Note: Java has many log frameworks. Do not worry if the preceding log tool is confusing. Generally, SpringBoot works well without changing the log dependency.
26.1Log format
SpringBoot's default log output 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 following items will be output:
-
- Date and Time-precision in milliseconds, easy to sort
- Log Level: ERROR, WARN, INFO, DEBUG, TRACE
- Process ID
- ----- Delimiter used to distinguish real log information
- Thread name-enclosed in square brackets (may be truncated during console output)
- Log name-Resource class name (usually simplified)
- Log Information
Note: The LogBack tool does not have the FATAL level (this level will be mapped to ERROR)
26.2Console output
The default log configuration log information will be output to the console. By default, ERROR, WARN, and INFO information is recorded. You can also enable the debug mode by adding the -- debug tag when you start the application.
$ java -jar muapp.jar --debug
Note: You can also add debug = true in the application. properties configuration file to achieve the same purpose.
When the debug mode is used, the core recorder (including embedded containers, Hibernate, and SpringBoot) is configured to output more information. In debug mode, your application is not configured to record all information at the DEBUG level.
Alternatively, you can enable the trace mode in two similar ways, which will output the tracing logs of the core recorder.
26.2.1Color output
If your terminal supports ANSI, the color output log information can improve readability. You can set spring. output. ansi. enabled to an appropriate value to overwrite the automatically obtained value.
Color encoding uses % clr for configuration. In the simplest form, the converter colors the output based on the log level, for example:
%clr(%5p)
The matching relationship between Log Level and coloring is as follows:
Level (Log Level) |
Color) |
FATAL |
Red |
ERROR |
Red |
WARN |
Yellow |
INFO |
Green |
DEBUG |
Green |
TRACE |
Green |
In addition, you can add an option for the converter to specify the color or style. For example, set the text output to Yellow:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
The following are colors or styles supported by SpringBoot:
-
- Blue
- Cyan
- Faint
- Green
- Magenta
- Red
- Yellow
26.3File output
By default, SpringBoot only outputs logs to the console rather than files. If you want to output logs not only to the console, but also to files, you need to set the logging. file or logging. path attribute in the application. properties file.
The following table shows how to use the logging. * attribute together:
Logging. file |
Logging. path |
Example |
Description |
(None) |
(None) |
|
Output only to the console |
Specified file |
(None) |
My. log |
Output to the specified file. The name can be accurate or relative to the current directory. |
(None) |
Specify directory |
/Var/log |
Output spring. log to the specified directory. The name can be accurate or relative to the current directory. |
Table 26.1 log attributes
By default, log files record the log information at the console output, ERROR, WARN, and INFO levels, and re-create a file to record it when it reaches 10 MB.
Note: The SpringBoot logging system completes initialization early in the application lifecycle. Therefore, attributes in the configuration file loaded with the @ PropertiesSource annotation do not play an appropriate role (not effective ).
Note: The log attributes are irrelevant to the actual log infrastructure. Therefore, the specified configuration content (like the LogBack. configurationFile attribute of logback) is not managed by SpringBoot.
26.4Log Level
All supported log systems are available in Spring Environment (the Environment is like the application in Spring boot. properties. level. * = LEVEL, where LEVEL can be TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. You can use the attribute logging. level. root to configure the root log. For example: (IN application. properties)
1 logging.level.root=WARN2 logging.level.org.springframework.web=DEBUG3 logging.level.org.hibernate=ERROR
Note: by default, SpringBoot redefines the Thymeleaf template INFO information so that it can be recorded by DEBUG-level logs. This helps reduce the chaos in the standard log output. For details about how to redefine your configuration, see LevelRemappingAppender.
26.5Custom log Configuration
Various log systems can be activated by appropriate libraries in the classless path and then by logging in the classless path or Spring environment. the appropriate configuration file is customized under the path specified by the config attribute.
You can use the org. springframework. boot. logging. LoggingSystem system attribute to force SpringBoot to use a specified log system. The value is the fully qualified name for implementing LoggingSystem. You can also set it to none. The log function is completely invalid.
Note: The log system completes the initialization before ApplicationContext initialization. Therefore, the @ PropertiesSources in @ Configuration cannot be used to control the log system. The system attributes and the basic configuration file of SpringBoot are in good effect.
Based on your log system, the following files will be loaded:
Logging System |
Customization (custom) |
LogBack |
Logback-spring.xml; ogback-spring.groovy; logback. xml; logback. groovy |
Log4j2 |
Log4j2-spring.xml; log4j2. xml |
JDK (Java Util Logging) |
Logging. properties |
NOTE: If possible, we recommend that you use the-spring variable to configure the log configuration (for example, logBack-spring.xml rather than logBack. xml) if you use the previous configuration path, Spring cannot completely control the log initialization.
Note: There are some known class loading issues in Java Util logs, which cause problems when running from executable jar. We recommend that you try to avoid using it.
To help customization, some other attributes are transferred from the Spring environment to the system attributes:
Spring Environment |
System Property |
Comments (description) |
Logging. exception-conversion-word |
LOG_EXCEPTION_CONVERSION_WORD |
Conversion term used for recording exceptions |
Logging. file |
LOG_FILE |
Use in the default log Configuration |
Logging. path |
LOG_PATH |
Use in the default log Configuration |
Logging. pattern. console |
Lele_log_pattern |
Log format used for log output in the console (only the default LogBack configuration is supported) |
Logging. pattern. file |
FILE_LOG_PATTERN |
Log format used for file output logs (only the default LogBack configuration is supported) |
Logging. pattern. level |
LOG_LEVEL_PATTERN |
The style used to render the log level (% 5 p by default) (only the default LogBack configuration is supported) |
PID |
PID |
Current process ID (if possible, it is not defined as a system environment variable) |
All supported log systems can view system properties when parsing their configuration files. For example, default configuration in the spring-boot.jar.
If you want to use placeholders in a log attribute, you need to use the SpringBoot syntax instead of the underlying framework syntax. In addition, if you are using LogBack, you need to use: to separate an attribute from its default value, rather :-.
Overwrite the LOG_LEVEL_PATTERN attribute (or logging in LogBack. pattern. level Attribute), you can add MDC (Mapped Diagnostic Context ing debugging Context in the log line: A function provided by log4j and logback to conveniently record logs under multi-thread conditions) and ad-hoc (point-to-point) content. For example, if you use logging. pattern. level = user: % X {user} % 5 p configuration, the default log output format will contain an MDC entry named user (of course, it must exist for display). For example:
2015-09-30 12:30:04.031 user:juergen INFO 22174 --- [ nio-8080-exec-0] demo.Controller Handling authenticated request
26.6LogBack Extension
SpringBoot provides many extended attributes for LogBack for advanced configuration. You can use these attributes in your logback-spring.xml profile.
Note: Because the logback. xml file is loaded too early (earlier than the startup of the SpringBoot application), you cannot use these extended attributes in it, you need to use the logBack-spring.xml file or the logging. config attribute.
Note: These extended attributes cannot be used with the LogBack configuration scanner (cannot be scanned). If you try to do so, you will get an error record similar to the following:
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 ElementPath is [[configuration][springProfile]
26.6.1Customized Configuration
The <springProfile> label can be used to execute certain configurations in the Spring configuration file based on conditions or exclude them. It can be used anywhere in the <congfiguration> element. Use the name attribute to specify which configuration plane can be executed. Multiple profiles can be specified by commas.
1 <springProfile name="staging"> 2 <!-- configuration to be enabled when the "staging" profile is active --> 3 </springProfile> 4 5 <springProfile name="dev, staging"> 6 <!-- configuration to be enabled when the "dev" or "staging" profiles are active --> 7 </springProfile> 8 9 <springProfile name="!production">10 <!-- configuration to be enabled when the "production" profile is not active -->11 </springProfile>
26.6.2Environment attributes
<SpringProperty> Labels allow you to extract attributes from the Spring environment for LogBack. This is effective when you obtain the attribute value about LogBack configuration from application. properties. This label works in a similar way as the <properties> label of LogBack. Instead of directly setting a value, it specifies the source attribute of the attribute ). You can use the scope attribute to save the attribute, otherwise it will only exist locally. If you want to specify a reliable value in case it is not configured, you can use the defaultValue attribute to specify a default value.
1 <springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>2 <appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">3 <remoteHost>${fluentHost}</remoteHost>4 ...5 </appender>
Note: the value of the source attribute must be in the string format (for example, my. property. name ). However, you can use easy rules to add attributes to the environment.