在springboot项目里使用logback报如下错误

14:25:46,762 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:72 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]
14:25:46,762 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:81 - no applicable action for [springProperty], current ElementPath  is [[configuration][springProperty]]

解决方式

官方提供了解决方案:

  • 方法一

    将logback配置文件名由logback.xml改为logback-spring.xml

  • 方法二

    在项目配置文件,如application.properties中指定logging.config=classpath:logback.xml

原因

在这里插入图片描述

springboot提供了一些logback扩展功能。如果日志配置文件的名称是logback.xml,logback配置会先于Spring之前加载,导致无法使用扩展功能。

如果logback配置文件中使用的变量是springboot项目中的,则过早加载logback配置文件会获取不到这些变量。比如logback.xml中有这样的配置

<springProperty scope="context" name="LOG_PATH" source="log.path"/>

log.path是配置在 application.properties 中的,如果 logback.xml 先于application.properties 加载,自然因得不到log.path而报错。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐