In depth understanding of Spring Boot log management
- 2020-06-03 06:39:43
- OfStack
preface
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 contentsConsole output
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 2.
application.properties
In the configuration
debug=true
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.
Colorful output
If your terminal supports ANSI, setting color output will make the log more readable. Through the
application.properties
Set in the
spring.output.ansi.enabled
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
application.properties
In the configuration
logging.file
or
logging.path
Properties.
1.
logging.file
, which can be either an absolute or relative path. Such as:
logging.file=my.log
2.
logging.path
, the setting directory will be created in this directory
spring.log
File and write log contents such as:
logging.path=/var/log
Log files are truncated at the size of 10Mb, resulting in new log files with default levels of ERROR, WARN, INFO *
Level control
In Spring Boot only in
application.properties
To configure the level control of completion logging.
Configuration format:
logging.level.*=LEVEL
1.
logging.level
: Log level control prefix, * for package name or Logger name
2.LEVEL: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
For example:
logging.level.com.juzi=DEBUG com.juzi
All class under the package are output at the DEBUG level
logging.level.root=WARN
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
2.Log4j: Es179EN4ES180en-ES181en
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
application.properties
Configure the following parameters to control the output format:
1.
logging.pattern.console
: Define the style of output to the console (JDK Logger is not supported)
2.
debug=true
0
: Define the style of output to the file (JDK Logger not supported)
conclusion
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.