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

What is default CONSOLE_LOG_PATTERN used for Spring Boot logging and where to find it?

Spring 引导参考文档4.6. Custom Log Configuration 说明了默认系统属性表示在控制台上使用的默认日志记录模式(仅支持默认 Logback 设置)。

我想所有 Spring 引导框架用户都熟悉默认日志行:

2020-08-04 12:00:00.000  INFO 24568 --- [           main] c.c.MyWonderfulSpringApplication          : The following profiles are active: local

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

我刚刚发现此配置在 Spring 引导项目下的 DefaultLogbackConfiguration 文件中可用:

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}";

要查找特定 Spring 引导版本的模式,可以:

  • 浏览 GitHub 处可用的源文件:Spring Boot 2.3.x
  • 在 IntelliJ Idea 中按 2x Left Shift 并全文搜索 DefaultLogbackConfiguration

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

如果您正在使用 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>

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