完整報錯信息如下:
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對應的轉換器類了。