In depth understanding of Spring Boot log management
- 2020-06-03 06:39:43
Spring Boot USES Commons Logging for all internal logs, but the default configuration also provides support for commonly used logs,
For example, Java Util Logging, Log4J, Log4J2 and Logback. Each Logger can be configured to output log content using the console or file.
Log output format
2016-08-19 10:22:04.233 INFO 7368 --- [ main] com.juzi.AsyncTest : Started AsyncTest in 10.084 seconds (JVM running for 12.545)
The output content elements are as follows:Time and date - accurate to milliseconds Log levels - ERROR, WARN, INFO, DEBUG or TRACE Process ID The delimiter -- identifies the beginning of the actual log Thread name - enclosed in square brackets (may truncate console output) Logger name - usually the name of the class used in the source code Log contents
By default, ERROR, WARN, and INFO levels of logging are configured in Spring Boot to log to the console.
We can switch to the DEBUG level in two ways:
Add debug flags after running the command, such as:
$ java -jar myapp.jar �debug
In the configuration
When this attribute is set to true, core Logger (containing embedded containers, hibernate, spring) will output more, but your own application logs will not output DEBUG.
If your terminal supports ANSI, setting color output will make the log more readable. Through the
Set in the
Parameters to support.
1.NEVER: Disable ANSI-ES68en output (default)
2.DETECT: It will check whether the terminal supports ANSI. If it does, it will use color output (recommended).
3.ALWAYS: ANSI-ES79en format is always used for output. If the terminal is not supported, there will be a lot of interference information, which is not recommended
The output file
The default configuration of Spring Boot is only printed to the console and is not recorded in a file, but we usually need to document it in a production environment.
To increase the file output, you need to
In the configuration
, which can be either an absolute or relative path. Such as:
, the setting directory will be created in this directory
File and write log contents such as:
Log files are truncated at the size of 10Mb, resulting in new log files with default levels of ERROR, WARN, INFO *
In Spring Boot only in
To configure the level control of completion logging.
: Log level control prefix, * for package name or Logger name
2.LEVEL: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
All class under the package are output at the DEBUG level
The root log is output at the WARN level
Customize log configuration
Since logging service 1 is typically initialized before ApplicationContext is created, it does not have to be controlled through 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 filenames to load correctly by following the following rules:
1.Logback: ES163en-ES164en.xml, ES166en-ES167en
3. Log4j2: log4j2 - spring xml, log4j2. xml
4.JDK (Java Util Logging) : logging.properties
Spring Boot The official recommendation is to use the file name with -ES218en as your log configuration (es219EN-ES220en.xml instead of ES222en.xml)
Customize the output format
Can be accessed in Spring Boot
Configure the following parameters to control the output format:
: Define the style of output to the console (JDK Logger is not supported)
: Define the style of output to the file (JDK Logger not supported)
The above is the whole content of this article, I hope the content of this article can bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.