Springboot项目启动报错:Failed to create converter for [%clr] keyword

完整报错信息如下:

Exception in thread "main" java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.pattern.parser.Compiler@7a362b6b - There is no conversion class registered for composite conversion word [clr]
ERROR in ch.qos.logback.core.pattern.parser.Compiler@7a362b6b - Failed to create converter for [%clr] keyword
ERROR in ch.qos.logback.core.pattern.parser.Compiler@7a362b6b - There is no conversion class registered for composite conversion word [clr]
ERROR in ch.qos.logback.core.pattern.parser.Compiler@7a362b6b - Failed to create converter for [%clr] keyword
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:166)
	at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:82)
	at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:114)
	at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:264)
	at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:237)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:173)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
	at com.kaka.jtest.springboot.Application.main(Application.java:24)
前因

看到网上说,可以指定日志的颜色。使用方法:在日志配置文件中的pattern节点中,加上%clr即可,格式:%clr(日志内容){颜色}

支持的颜色:

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

于是我就在控制台输出试了试,没想到项目起不来了。日志配置如下,主要看pattern中的内容:

<!--1.控制台输出日志 ConsoleAppender-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoder 默认配置为PatternLayoutEncoder -->
    <encoder>
        <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){green}---%magenta([%thread])---%clr(%-5level)---%blue([%c])---%green([%L])---[traceId:%X{traceId}]---[%msg]%n</pattern>
    </encoder>
</appender>
解决

日志配置文件(logback-spring.xml)中,引入springboot的默认日志配置。

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

pattern节点中使用百分号(%)开头的关键字,都会对应一个转换器。这些转换器可以自己扩展开发,但需要在配置文件中指定关键字对应的转换器类。这也就能解释报错中的信息:

There is no conversion class registered for composite conversion word [clr]

所以springboot中颜色关键字clr,也应该指定一个对应的转换器类,否则就会报找不到对应的转换器的错误!其实clr的转换器类springboot已经开发的,我们仅需指定下即可。我们看下引入的文件:org/springframework/boot/logging/logback/defaults.xml 就明白了。
在这里插入图片描述
我们引入的这个defaults.xml文件中,springboot已经给我们制定好clr对应的转换器类了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章