Spring Cloud Spring Boot mybatis Distributed Microservice Cloud Architecture (37) Log Management

Spring Boot uses Commons Logging for all internal logs , but the default configuration also provides support for common logs such as: Java Util Logging , Log4JLog4J2 and Logback . Each Logger can be configured to use console or file output log content.

format log

The default log output is as follows:

2016-04-13 08:23:50.120  INFO 37397 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.11.Final}

The output content elements are as follows:

  • time date - accurate to milliseconds
  • Log level — ERROR, WARN, INFO, DEBUG or TRACE
  • Process ID
  • delimiter -  --- identifies the start of the actual log
  • Thread name - in square brackets (may truncate console output)
  • Logger name — usually the class name of the source code
  • log content

console output

By default, Spring Boot configures the log output to the console ERRORwith WARNand level.INFO

We can switch to DEBUGlevels in two ways:

  • --debugAdd the flag after running the command , like:$ java -jar myapp.jar --debug
  • In application.propertiesthe configuration debug=true, when this property is set to true, the core Logger (including embedded container, hibernate, spring) will output more content, but the log of your own application will not be output as DEBUG level.

colorful output

If your terminal supports ANSI, setting colored output will make the log more readable. Supported by application.propertiessetting spring.output.ansi.enabledparameters in .

  • NEVER: Disable ANSI-colored output (default)
  • DETECT: Will check whether the terminal supports ANSI, and if so, use color output (recommended)
  • ALWAYS: Always use ANSI-colored format for output. If the terminal does not support it, there will be a lot of interference information, which is not recommended.

file output

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

To increase file output, you need application.propertiesto configure logging.fileor logging.pathproperties in .

  • logging.file, the setting file, can be an absolute path or a relative path. Such as:logging.file=my.log
  • logging.path, set the directory, the spring.log file will be created in this directory, and the log content will be written, such as:logging.path=/var/log

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

level control

In Spring Boot, you only need application.propertiesto configure the level control of logging to complete.

Configuration format:logging.level.*=LEVEL

  • logging.level: The log level control prefix, which *is the package name or Logger name
  • LEVEL:选项TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

Example:

  • logging.level.com.didispace=DEBUG: com.didispaceAll classes under the package are output at DEBUG level
  • logging.level.root=WARN: root log output at WARN level

Custom log configuration

Since the logging service is generally initialized before the ApplicationContext is created, it does not have to be controlled by 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 configuration file names according to the following rules, and they will be loaded correctly:

  • Logback:logback-spring.xmllogback-spring.groovylogback.xml,logback.groovy
  • Log4j:log4j-spring.propertieslog4j-spring.xml, ,  log4j.properties,log4j.xml
  • Log4j2:log4j2-spring.xmllog4j2.xml
  • JDK (Java Util Logging):logging.properties

Spring Boot officially recommends to use -springthe file name with it as your log configuration (if you use logback-spring.xmlit instead logback.xml)

Custom output format

In Spring Boot, the application.propertiesoutput format can be controlled by configuring the following parameters:

  • logging.pattern.console: defines the style of output to the console (JDK Logger is not supported)
  • logging.pattern.file: defines the pattern of output to file (JDK Logger is not supported)

source code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856496&siteId=291194637