关于spring-boot中logback日志配置引用spring环境变量的使用方式
在使用spring-boot开发过程中,可以通过修改logback配置文件属性声明,就可以引用到spring环境变量,如application.properties中的变量,能方便的打印一些有帮助的信息,便于我们分析问题
官方文档说明:https://docs.spring.io/spring-boot/docs/2.4.5/reference/htmlsingle/#boot-features-logback-extensions
使用方式
1. 修改spring-boot工程中的application.yaml配置文件(application.properties配置同理),来加载logback xml配置文件
2.修改logback的xml配置文件,使用<springProperty>
标签来声明引用application.yaml中的哪个变量,声明后就可以在appender中使用变量了(类似<property>标签使用方式)
示例配置:https://github.com/netbuffer/spring-boot-demo/blob/logback/src/main/resources/logback-sbd.xml
运行效果
运行工程后可以看到${appName}
的插值已经被替换为spring.application.name
环境变量输出了

示例工程:https://github.com/netbuffer/spring-boot-demo/tree/logback