Springboot logback-spring.xml无法加载
现象
SpringBoot项目,启动后,日志文件没有出现在logback-spring.xml指定的目录下
且启动日志开头如下:
“C:Program FilesJavajdk1.8.0_144binjava”…
Connected to the target VM, address: ‘127.0.0.1:52494’, transport: ‘socket’
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback-test.xml]
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Could NOT find resource [logback.groovy]
18:34:58,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] – Found resource [logback.xml] at [jar:file:/D:/mvn_repo/xxx-1.0.2.jar!/logback.xml]
18:34:58,014 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@707194ba – URL [jar:file:/D:/mvn_repo/yyy-1.0.2.jar!/logback.xml] is not of type file
18:34:58,112 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction – Will scan for changes in [jar:file:/D:/mvn_repo/zzz-1.0.2.jar!/logback.xml]
logback-spring.xml文件一直未改动过,如下:
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%n${logPath}/server.%d.log %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%nUTF-8ERRORACCEPTDENY${logPath}/server.%d.error %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} %line - %msg%nUTF-8
原因分析
1.配置文件位置正确
classes/ ├── application.properties ├── com │ └── nero │ └── test │ ├── api │ │ ├── Api.class
2.分析启动日志,分析可能是依赖的jar中有logback.xml配置,冲突导致的问题
解决方案
在application.properties中加入配置:
logging.config=classpath:logback-spring.xml
通过该配置,强制指定日志的配置文件,问题得以解决
springboot logback-spring.xml 配置, 使用不同配置文件中的配置
application.yml
spring: profiles: active: prod # active 哪一个配置文件
application-dev.yml
logging: file: path: d:/var/log
application-prod.yml
logging: file: path: /var/log/question
logback-spring.xml
wechat-service${log.colorPattern}${log.path}/info/info.%d{yyyy-MM-dd}.log${log.maxHistory}${log.pattern}INFOACCEPTDENY${log.path}/error/error.%d{yyyy-MM-dd}.log${log.pattern}ERRORACCEPTDENY
从application-xxx.yml 中拿配置
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。