Home  >  Article  >  Java  >  Detailed explanation about Spring Boot’s log management

Detailed explanation about Spring Boot’s log management

巴扎黑
巴扎黑Original
2017-08-22 16:43:141708browse

PrefaceTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Spring Boot uses Commons Logging in all internal logs , but the default configuration also provides support for commonly used logs, TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
such as: Java Util Logging, Log4J, Log4J2 and Logback. Each Logger can be configured to use the console or file to output log content. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Log output formatTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

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:TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

  • ##Time and date - accurate to milliseconds

  • Log level - ERROR, WARN, INFO, DEBUG or TRACE

  • Process ID

  • Separator ― ― Identifies the start of the actual log

  • Thread name ― enclosed in square brackets (may truncate console output)

  • Logger name ― Normally Use the class name of the source code

  • Log content

Console outputTFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

In Spring Boot, ERROR, WARN, and INFO level logs are configured by default to be output to the console.

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

We can switch to DEBUG level in two ways:

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1. Add the debug flag after running the command, such as:

$ java -jar myapp.jar debug TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

2. Configure in

application.properties debug=true, when this property is set to true, the core Logger (including embedded containers, hibernate, spring) will output more content, but your own application logs will not be output at DEBUG level. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Colorful OutputTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

If your terminal supports ANSI, setting colored output will make the logs more readable. Supported by setting the

spring.output.ansi.enabled parameter in application.properties. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

1.NEVER: Disable ANSI-colored output (default item)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

2.DETECT: Will check whether the terminal supports ANSI, if so, use color output (recommended item)

TFhHTML5 Chinese Learning Net - HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

3.ALWAYS: Always use ANSI-colored format output, if the terminal does not support it , there will be a lot of interfering information, and it is not recommended to use

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

File OutputTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The default configuration of Spring Boot will only be output to the console and will not be recorded in a file. However, we usually need to record it in a file when using it in a production environment.

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

To increase file output, you need to configure

logging.file or in application.properties logging.pathProperty. TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1.

logging.file, set the file, which can be an absolute path or a relative path. For example: logging.file=my.logTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.

logging.path, set the directory, the spring.log file will be created in the directory, and the log content will be written, such as: logging.path =/var/logTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

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 *

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Level ControlTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

In Spring Boot, you only need to configure it in

application.properties to complete the level control of logging. TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Configuration format:

logging.level.*=LEVELTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

1.logging.level: Log level control prefix, * is the package name or Logger nameTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.LEVEL: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFFTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Example: TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

##logging .level.com.juzi=DEBUG com.juziAll classes under the package are output at DEBUG levelTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

logging.level.root=WARN The root log is output at WARN levelTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Custom log configurationTFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Since the log service is generally initialized before the ApplicationContext is created, it does not have to pass Spring configuration file control.

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.
TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

According to different log systems, you can organize the configuration file name according to the following rules, and it will be loaded correctly:

TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network

1.Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy logback log configuration

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

2.Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j .xml

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

3.Log4j2: log4j2-spring.xml , log4j2.xml

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network##TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network
4.JDK (Java Util Logging ): logging.properties

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Spring Boot official recommendation is to give priority to the file name with -spring as your log configuration (such as using logback-spring .xml, not logback.xml)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

Customized output format

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning NetworkIn Spring Boot, you can control the output format by configuring the following parameters in

application.properties

: TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network 1.

logging.pattern.console

: Define the style of output to the console (JDK Logger is not supported)TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network TFhHTML5 Chinese Learning Network - HTML5 Pioneer Learning Network
2.

logging.pattern.file

: Define the style of output to the file (JDK Logger is not supported)

TFhHTML5 Chinese Learning Network-HTML5 Pioneer Learning Network

The above is the detailed content of Detailed explanation about Spring Boot’s log management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn