Spring Boot 教程

Spring Boot 笔记

no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]] 错误信息原因及解决方法

Spring Boot 笔记 Spring Boot 笔记


spring boot 的 logback.xml 配置中,根据不同的环境配置了不同日志级别及 appender 等信息,但是服务在启动时,报如题错误,原因何在?以及如何解决?

现象

logback.xml 配置的问题部分如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M %L - %msg %n</Pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>

    <!-- fileAppender, errorAppender -->
    ......

    <springProfile name="dev,staging,prod">
        <root level="INFO">
            <appender-ref ref="consoleAppender"/>
            <appender-ref ref="fileAppender"/>
            <appender-ref ref="errorAppender"/>
        </root>
    </springProfile>

    <springProfile name="local">
        <root level="INFO">
            <appender-ref ref="consoleAppender"/>
        </root>
    </springProfile>

</configuration>

本身想通过的 springProfile 的不同环境参数来设置不同的日志配置,程序启动时,会报如下错误,虽不影响服务启动,错误信息具体如下:

-ERROR in ch.qos.logback.core.joran.spi.Interpreter@41:44 - no applicable action for [springProfile], current ElementPath  is [[configuration][springProfile]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@42:28 - no applicable action for [root], current ElementPath  is [[configuration][springProfile][root]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@43:50 - no applicable action for [appender-ref], current ElementPath  is [[configuration][springProfile][root][appender-ref]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@44:47 - no applicable action for [appender-ref], current ElementPath  is [[configuration][springProfile][root][appender-ref]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@45:48 - no applicable action for [appender-ref], current ElementPath  is [[configuration][springProfile][root][appender-ref]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@49:33 - no applicable action for [springProfile], current ElementPath  is [[configuration][springProfile]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@50:28 - no applicable action for [root], current ElementPath  is [[configuration][springProfile][root]]
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:50 - no applicable action for [appender-ref], current ElementPath  is [[configuration][springProfile][root][appender-ref]]

原因

从报错的信息可以看出,logback 没有识别出 springProfile 的配置,其实是因为项目中配置的日志文件的名称是 logback.xmllogback 会在 spring 之前加载日志配置,这时日志文件中的 springProfile 的配置是无效的。

解决方法

根据官方文档的解决方法,将 logback.xml 改为 logback-spring.xml 即可。