Log Management of Spring Boot

Log Management of Spring Boot

 

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

 

format log

 

The default log output is as follows:


 

The output content elements are as follows:

 

      1. Time and date - accurate to milliseconds

      2. Log level — ERROR, WARN, INFO, DEBUG or TRACE

      3. Process ID

      4. Separator --- --- Identifies the beginning of the actual log

      5. Thread name - in square brackets (may truncate console output)

      6. Logger name - usually the class name of the source code

      7. Log content

 

 

 

console output

 

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

 

We can switch to DEBUG level in two ways:

 

      1. Add the --debug flag after running the command, such as: $ java -jar myapp.jar --debug

 

      2. Configure debug=true in application.properties. 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

 

如果你的终端支持ANSI,设置彩色输出会让日志更具可读性。通过在application.properties中设置spring.output.ansi.enabled参数来支持。

 

      1. NEVER:禁用ANSI-colored输出(默认项)

      2. DETECT:会检查终端是否支持ANSI,是的话就采用彩色输出(推荐项)

      3. ALWAYS:总是使用ANSI-colored格式输出,若终端不支持的时候,会有很多干扰信息,不推荐使用

 

 

 

文件输出

 

Spring Boot默认配置只会输出到控制台,并不会记录到文件中,但是我们通常生产环境使用时都需要以文件方式记录。

 

若要增加文件输出,需要在application.properties中配置logging.filelogging.path属性。

 

      1. logging.file,设置文件,可以是绝对路径,也可以是相对路径。如:logging.file=my.log

      2. logging.path,设置目录,会在该目录下创建spring.log文件,并写入日志内容,如:logging.path=/var/log

 

日志文件会在10Mb大小的时候被截断,产生新的日志文件,默认级别为:ERROR、WARN、INFO

 

 

 

级别控制

 

在Spring Boot中只需要在application.properties中进行配置完成日志记录的级别控制。

 

配置格式:logging.level.*=LEVEL

 

      1. logging.level:日志级别控制前缀,*为包名或Logger名

      2. LEVEL:选项TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

 

举例:

 

      1. logging.level.com.didispace=DEBUG:com.didispace包下所有class以DEBUG级别输出

      2. logging.level.root=WARN:root日志以WARN级别输出

 

 

 

自定义日志配置

 

由于日志服务一般都在ApplicationContext创建前就初始化了,它并不是必须通过Spring的配置文件控制。因此通过系统属性和传统的Spring Boot外部配置文件依然可以很好的支持日志控制和管理。

 

根据不同的日志系统,你可以按如下规则组织配置文件名,就能被正确加载:

 

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

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

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

      4. JDK (Java Util Logging):logging.properties

 

Spring Boot官方推荐优先使用带有-spring的文件名作为你的日志配置(如使用logback-spring.xml,而不是logback.xml)

 

 

 

自定义输出格式

 

在Spring Boot中可以通过在application.properties配置如下参数控制输出格式:

 

      1. logging.pattern.console:定义输出到控制台的样式(不支持JDK Logger)

 

      2. logging.pattern.file:定义输出到文件的样式(不支持JDK Logger)

 

 

 

参考:

http://blog.didispace.com/categories/Spring-Boot/page/2/

Guess you like

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