【问题标题】:spring-boot application get warning: CONSOLE_LOG_PATTERN_IS_UNDEFINED [duplicate]spring-boot 应用程序收到警告:CONSOLE_LOG_PATTERN_IS_UNDEFINED [重复]
【发布时间】:2018-09-27 12:05:50
【问题描述】:

有一个 spring-boot (2.0.0.RELEASE) 应用程序作为 spring-cloud (Finchley .M9) 集群。
在启动时,它总是打印以下行:

CONSOLE_LOG_PATTERN_IS_UNDEFINEDCONSOLE_LOG_PATTERN_IS_UNDEFINEDCONSOLE_LOG_PATTERN_IS_UNDEFINED


(以下是配置)

application.yml:

## spring-boot configuration,

# logging
logging:
  file: "log/hello.log"
  pattern:
    console: "[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level [%t] [%logger - %line]: %m%n"
    file: "[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level [%t] [%logger - %line]: %m%n"
  level:
    root: INFO

spring:
  application:
    name: eric-sc-hello

server:
  port: 9191

management:
  endpoints:
    web:
      exposure:
    include: "*"

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

eric-sc-hello:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:9191,localhost:9192,localhost:9193
    ServerListRefreshInterval: 15000

logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <charset>UTF-8</charset>
      <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
    </encoder>
  </appender>
  <appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <file>${LOG_FILE}</file>
    <rollingPolicy
      class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>10</maxIndex>
    </rollingPolicy>
    <triggeringPolicy
      class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>10MB</MaxFileSize>
    </triggeringPolicy>
  </appender>

  <root level="INFO">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

警告说 CONSOLE_LOG_PATTERN 未定义 3 次。

但是logging.pattern.console是在application.yml中定义的,并且console中的日志行是以指定的格式打印出来的。

那么,为什么会弹出警告,如何删除它?


@更新

我已经找到了一些解决方案,并在此处添加了答案:https://*.com/a/62846599/

【问题讨论】:

    标签: java spring-boot logback spring-cloud


    【解决方案1】:

    我无法解释为什么会发生这种情况,但是如果您将 logging.config 属性设置为其他例如my-project-logback.xml 并且不要使用 logback-spring.xml 名称 - 在没有 CONSOLE_LOG_PATTERN_IS_UNDEFINED 消息的情况下一切正常。文档允许这样做。

    来自Spring Boot Logging documentation

    因为标准的 logback.xml 配置文件加载过早,不能在里面使用扩展。您需要使用 logback-spring.xml 或定义 logging.config 属性。

    【讨论】:

      【解决方案2】:

      我的回答与您的问题没有直接关系,但我在Spring Boot 2.x 中使用默认的org/springframework/boot/logging/logback/console-appender.xml 附加程序时看到了类似的问题。

      因此,您可以直接在logback-spring.xml 配置中包含CONSOLE_LOG_PATTERN 定义,而不是在应用程序配置中定义logging.pattern.console

      e.g. 默认配置会像这样包含在内。

      <configuration>
       <include resource="org/springframework/boot/logging/logback/defaults.xml">
      ...
      

      【讨论】:

        【解决方案3】:

        您需要像这样在 logback-spring.xml 中明确定义 Spring 属性:

            <configuration>
                <springProperty name="CONSOLE_LOG_PATTERN" source="logging.pattern.file"/>
        
              .... rest of configuration ....
        

        【讨论】: