【Spring Boot 官方文档】26、Log日志

简介:

Spring Boot所有内部日志使用Apache的Commons Logging组件,同时也开放了底层的日志实现。

Spring Boot为3种日志组件Java Util Logging,Log4J2,Logback提供了默认配置,而且为每一种预设了控制台输出,并提供文件输出可选。

如果使用Spring Boot的starters组件,默认使用Logback组件。

Spring Boot提供了适当的Logback路由,以保证依赖库使用Java Util Logging, Commons Logging, Log4J, or SLF4J的时候,能正常运作。

1、日志格式

默认日志格式如下

2014-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

其中内容包含:

日期和时间

日志等级:ERROR, WARN, INFO, DEBUG, or TRACE.

进程ID

---:分隔符

线程名称: 包含在方括号中

日志器名:通常是来源类

日志信息

2、控制台输出

默认日志配置会在写入消息时将消息回传给控制台。

默认情况下,将记录ERROR级别,WARN级别和INFO级别的消息。您还可以通过使用--debug标志启动应用程序来启用“调试”模式。

运行时:

$ java -jar myapp.jar --debug

也可以在application.properties文件中设置

debug=true

当启用调试模式时,是代表将一系列日志器(如嵌入式容器,Hibernate和Spring Boot)配置为输出更多信息。

启用调试模式不是说将应用程序中DEBUG级别记录所有消息都输出,而是说让系统中的一些日志器输出更多信息。

同样,可以通过--trace或者trace=true的形式,让系统中一些日志器输出跟踪信息。

控制颜色输出

如果终端支持ANSI,则可以通过输出带颜色的文字提高可读性。

增加设置

#输出彩色日志
spring.output.ansi.enabled=ALWAYS

[这部分未细看]

You can set spring.output.ansi.enabled to a supported value to override the auto detection.

Color coding is configured by using the %clr conversion word. In its simplest form, the converter colors the output according to the log level, as shown in the following example:

%clr(%5p)

The following table describes the mapping of log levels to colors:

Level Color

FATAL

Red

ERROR

Red

WARN

Yellow

INFO

Green

DEBUG

Green

TRACE

Green

Alternatively, you can specify the color or style that should be used by providing it as an option to the conversion. For example, to make the text yellow, use the following setting:

%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}

The following colors and styles are supported:

  • blue
  • cyan
  • faint
  • green
  • magenta
  • red
  • yellow

3、文件输出

默认情况下,Spring Boot只会在控制台输出日志信息。我们可以设置让Spring Boot把日志信息输出到文件中。

可以在application.properties文件中设置logging.filelogging.path属性

  • 2个属性都不设置时,则只在控制台输出日志
  • logging.file=my.log:写到特定文件中,可以是相对路径或者绝对路径
  • logging.path=/var/log:写到特定文件夹中,可以是相对路径或者绝对路径

默认情况下当文件达到10MB,会重新创建一个文件继续记录,默认记录ERRORWARN, INFO等级的日志。

  • 设置logging.file.max-size可以改变文件的大小限制
  • 默认下,之前的日志文件会一直保存,可以通过logging.file.max-history属性来设置保存多少的历史日志

注意2点:

  • 日志记录系统在应用程序生命周期的很早期就进行了初始化。因此,不能在属性文件中通过@PropertySource注释设置日志属性。
  • Spring Boot的日志属性是独立于基础组件的(如logback),所以组件的属性(如logback的logback.configurationFile)不被Spring Boot所管理,Spring Boot只管理自己的日志属性(上限讨论的属性)

4、日志等级

可以在Spring环境(例如application.properties)中为所有支持的日志系统设置日志记录等级。

通过logging.level.<logger-name>=<level> 进行设置,其中logger-name是调用logger-name时传入的名称(下面实例中了解),level是日志等级

level日志等级包括:TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF

可以通过logging.level.root=<level> 来进行统一配置

一个使用实例

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

5、自定义日志配置

各种日志系统,可以通过以下方式进行自定义配置:

  • 将适当的依赖类放置到类路径下
  • 然后创建一个对应的配置文件放在类路径根目录下;或使用Spring环境属性logging.config指明对应配置文件

可以通过org.springframework.boot.logging.LoggingSystem属性,强制Spring Boot使用特定的一个日志系统;

这个值必须是一个日志系统实现的全限定类名;

也可以设置为none,完全禁止Spring Boot的日志配置。

注意!因为日志系统在ApplicationContext之前创建,所以日志属性只能通过properties文件进行设置,无法通过Sprint@Configuration文件中使用@PropertySources设置。

根据指定的系统,回加载对应的配置文件

  • Logback:logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy

  • Log4j2:log4j2-spring.xml or log4j2.xml

  • JDK (Java Util Logging):logging.properties

建议使用-spring的命名方式

当使用Java Util Logging时,如果运行一个外部可执行jar,会产生类加载问题;所以推荐使用外部可执行jar时,禁止这个日志系统

为了实现自定义,Spring Boot将一些配置从Spring环境中转移到系统properties中去

Spring Environment System Property Comments

logging.exception-conversion-word

LOG_EXCEPTION_CONVERSION_WORD

The conversion word used when logging exceptions.

logging.file

LOG_FILE

If defined, it is used in the default log configuration.

logging.file.max-size

LOG_FILE_MAX_SIZE

Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)

logging.file.max-history

LOG_FILE_MAX_HISTORY

Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)

logging.path

LOG_PATH

If defined, it is used in the default log configuration.

logging.pattern.console

CONSOLE_LOG_PATTERN

The log pattern to use on the console (stdout). (Only supported with the default Logback setup.)

logging.pattern.dateformat

LOG_DATEFORMAT_PATTERN

Appender pattern for log date format. (Only supported with the default Logback setup.)

logging.pattern.file

FILE_LOG_PATTERN

The log pattern to use in a file (if LOG_FILE is enabled). (Only supported with the default Logback setup.)

logging.pattern.level

LOG_LEVEL_PATTERN

The format to use when rendering the log level (default %5p). (Only supported with the default Logback setup.)

PID

PID

The current process ID (discovered if possible and when not already defined as an OS environment variable).

如果想在日志记录属性中使用占位符,则应该使用Spring Boot的语法,而不是基础框架的语法。值得注意的是,如果你使用Logback,你应该使用:作为属性名和默认值之间的分隔符,而不是使用:- 。

[这段不太明白]

You can add MDC and other ad-hoc content to log lines by overriding only the LOG_LEVEL_PATTERN (or logging.pattern.level with Logback). For example, if you use logging.pattern.level=user:%X{user} %5p, then the default log format contains an MDC entry for "user", if it exists, as shown in the following example.

2015-09-30 12:30:04.031 user:someone INFO 22174 --- [  nio-8080-exec-0] demo.Controller
Handling authenticated request

fdsa

6、Logback扩展

这部分略,需要的自行查看Spring Boot官方文档

7、实际应用

猜你喜欢

转载自www.cnblogs.com/LiveYourLife/p/9221032.html