什么是用于 Spring Boot 日志记录的默认 CONSOLE_LOG_PATTERN 以及在哪里找到它?

Nik*_*las 12 java spring logback slf4j spring-boot

Spring Boot 参考文档4.6。自定义日志配置状态有关默认系统属性的状态,这些属性表示要在控制台上使用的默认日志记录模式(仅支持默认的 Logback 设置)。

  • 春季环境: logging.pattern.console
  • 系统属性: CONSOLE_LOG_PATTERN

我想所有 Spring Boot 框架用户都熟悉默认的日志行外观:

2020-08-04 12:00:00.000  INFO 24568 --- [           main] c.c.MyWonderfulSpringApplication          : The following profiles are active: local
Run Code Online (Sandbox Code Playgroud)

只要我想看看它的外观并获得定义我自己的灵感,我在哪里可以找到当前使用的 Spring Boot 版本的默认值?

Nik*_*las 16

我刚刚发现这个配置DefaultLogbackConfiguration在 Spring Boot 项目下的文件中可用:

private static final String CONSOLE_LOG_PATTERN = "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} "
            + "%clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} "
            + "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} "
            + "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
Run Code Online (Sandbox Code Playgroud)

要查找某个 Spring Boot 版本的模式,请执行以下任一操作:

  • 浏览 GitHub 上可用的源文件: Spring Boot 2.3.x
  • 在 IntelliJ Idea 按 2xLeft Shift和全文搜索DefaultLogbackConfiguration

我发现的来源是https://www.logicbig.com/tutorials/spring-framework/spring-boot/logging-console-pattern.html

  • 这是未分割的完整字符串: `%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p} ) %clr(${PID:- }){洋红色} %clr(---){微弱} %clr([%15.15t]){微弱} %clr(%-40.40logger{39}){青色} %clr(:){微弱} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}` (12认同)

小智 12

如果您使用的是logback-spring.xml,那么将以下内容添加到您的 xml 中将自动获取 spring 的控制台附加程序的默认 logback 配置。

<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
    <appender-ref ref="CONSOLE" />
</root>
Run Code Online (Sandbox Code Playgroud)

参考:https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/howto.html#howto-configure-logback-for-logging