1、ERROR in ch.qos.logback.core.joran.spi.Interpreter@11:86 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]]
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:179)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:313)
... 18 more
ch.qos.logback.core.joran.spi.Interpreter作用:扩展SAX DefaultHandler,根据预定义的模式调用各种操作(将xml配置文件翻译成日志对象)
异常提示:没有spring配置属性适合的action(对应logback.xml中的 springProperty 属性)
a、异常点:
xml配置: <springProperty scope="context" name="logging.path" source="logging.file.path"/>
对应代码:ch.qos.logback.core.joran.spi.EventPlayer#play,中正在解析该元素
b、为什么springProperty没有对应的Action ?
正常情况 List<Action> applicableActionList = ruleStore.matchActions(elementPath);应该是可以拿到springProperty对应的org.springframework.boot.logging.logback.SpringPropertyAction,然后进行spring属性解析的(即org.springframework.boot.logging.logback.SpringPropertyAction#getValue)
那为什么ruleStore中没有呢,分析下ch.qos.logback.core.joran.spi.Interpreter#ruleStore初始化的逻辑,断点定位解析并新增rule代码:ch.qos.logback.core.joran.GenericConfigurator#buildInterpreter
现象:加载rule的代码直接进入ch.qos.logback.classic.joran.JoranConfigurator中,并执行子类SpringBootJoranConfigurator扩展的springProperty相关性Action
未执行的代码逻辑:org.springframework.boot.logging.logback.SpringBootJoranConfigurator#addInstanceRules
c、为什么没有执行子类SpringBootJoranConfigurator的逻辑?
分析日志加载逻辑定位到:ch.qos.logback.classic.util.ContextInitializer#autoConfig
日志框架初始化看下会加载那些配置文件
发现日志上下文初始化时会自动加载这些文件,而此时spring环境的配置信息还没有加载(断点验证)
在ch.qos.logback.classic.util.ContextInitializer#configureByResource中使用了JoranConfigurator,此时的日志加载并不依赖spring环境的,所以在解析logback.xml时才会出现上述的报错
logback-spring.xml 在Spring应用程序运行时生效(启动的时候生效)
logback.xml 非Spring应用程序,(如main函数或测试类使用)logback.xml加载早于application.properties,
PS:如果在logback.xml使用了application.properties中变量时,会获取不到导致异常,可以改成logback-spring.xml解决。