【问题标题】:What is default CONSOLE_LOG_PATTERN used for Spring Boot logging and where to find it?用于 Spring Boot 日志记录的默认 CONSOLE_LOG_PATTERN 是什么以及在哪里可以找到它?
【发布时间】:2020-11-24 19:13:52
【问题描述】:

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

  • Spring环境: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

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

【问题讨论】:

    标签: java spring spring-boot logback slf4j


    【解决方案1】:

    如果您使用的是logback-spring.xml,那么将以下内容添加到您的 xml 将自动获取 spring 的控制台 appender 的默认 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

    【讨论】:

      【解决方案2】:

      我刚刚发现这个配置可以在 Spring Boot 项目下的 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 Boot 版本的模式,可以:

      • 浏览 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

      【讨论】:

      • 这是未拆分的完整字符串:%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}
      猜你喜欢
      • 2019-01-30
      • 2011-01-05
      • 2019-08-15
      • 2015-07-26
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多