log4j2.xml 使用 application.yml 配置的属性
创始人
2025-01-15 03:34:32
0

参考:https://www.jianshu.com/p/1f2427c87139

log4j2.xml 是不归 spring 管理的,所以也就没法读取到 application.yml 里面的配置了。 解决方式: 通过 spring 的 监听器(Listener)功能,将我们读取到的 application.yml 的日志路径设置到系统属性,然后在日志文件里面读取对应的系统属性就行了。

LoggingListener.java

通过 spring 的 监听器(Listener)功能,将我们读取到的 application.yml 的日志路径设置到系统属性,或者使用MDC

import org.apache.commons.lang3.StringUtils; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.stereotype.Component;   /**  * 读取yml配置传递到log4jXml中  *  * @author Clay  */ @Component public class LoggingListener implements ApplicationListener, Ordered {     /**      * 提供给日志文件读取配置的key,使用时需要在前面加上 sys:      */     private final static String LOG_PATH = "log.path";       /**      * spring 内部设置的日志文件的配置key      */     private final static String SPRING_LOG_PATH_PROP = "spring.log-file-path";       @Override     public void onApplicationEvent(ApplicationEvent applicationEvent) {           if (applicationEvent instanceof ApplicationEnvironmentPreparedEvent) {             ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) applicationEvent).getEnvironment();             String filePath = environment.getProperty(SPRING_LOG_PATH_PROP);             if (StringUtils.isNotEmpty(filePath)) {                 System.err.println("=================" + filePath);                 System.setProperty(LOG_PATH, filePath);             }         }     }       @Override     public int getOrder() {         // 当前监听器的启动顺序需要在日志配置监听器的前面,所以此处减 1         return LoggingApplicationListener.DEFAULT_ORDER - 1;     }   } 

application.yml

spring:    log-file-path: "F:/logs/" 

log4j2.xml

${sys:log.path} 

Application.java

这里没有贴出注解,关键于.addListeners(new LoggingListener())

内置Tomcat

public class Application {       public static void main(String[] args) {         SpringApplication application = new SpringApplication(Application.class);         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性         application.addListeners(new LoggingListener());         application.run(args);     }   } 

外置Tomcat

public class TomcatApplication extends SpringBootServletInitializer {       @Override     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性         builder.application().addListeners(new LoggingListener());         return builder.sources(Application.class);     }   } 

转至:https://blog.csdn.net/xiaokanfuchen86/article/details/126695797

相关内容

热门资讯

第十分钟总结!星悦山东辅助,反... 第十分钟总结!星悦山东辅助,反杀大厅辅助(辅助)果然是真的辅助器(哔哩哔哩)运反杀大厅辅助辅助工具,...
两分钟要领!青龙辅助3.0,新... 两分钟要领!青龙辅助3.0,新星游辅助软件(辅助)原来是有app(哔哩哔哩)1、打开软件启动之后找到...
四分钟妙招!好友赣南辅助器,人... 四分钟妙招!好友赣南辅助器,人海大厅脚本(辅助)总是有挂插件(哔哩哔哩)1、实时好友赣南辅助器透视辅...
第八分钟指引!九酷众游辅助,新... 第八分钟指引!九酷众游辅助,新卡农辅助(辅助)一贯是真的辅助器(哔哩哔哩)1、进入游戏-大厅左侧-新...
6分钟办法!乐酷大厅怎么安装,... 6分钟办法!乐酷大厅怎么安装,皇豪互众辅助软件是干嘛的(辅助)竟然有挂修改器(哔哩哔哩)所有人都在同...
一分钟方案!楚天游卡五星辅助器... 一分钟方案!楚天游卡五星辅助器,葫芦娃通杀辅助使用(辅助)一贯存在有工具(哔哩哔哩)亲,关键说明,葫...
一分钟操作!陕西三代辅助器,道... 一分钟操作!陕西三代辅助器,道游互娱辅助(辅助)一贯存在有辅助(哔哩哔哩)1、金币登录送、破产送、升...
第9分钟攻略!情怀宜春辅助,新... 第9分钟攻略!情怀宜春辅助,新九哥脚本下载(辅助)竟然真的有软件(哔哩哔哩)第9分钟攻略!情怀宜春辅...
六分钟法门!情怀宜春怎么开挂,... 六分钟法门!情怀宜春怎么开挂,微信小程序嘟嘟十三张脚本(辅助)切实真的有工具(哔哩哔哩)1)微信小程...
第五分钟举措!闲来贵州黑科技辅... 第五分钟举措!闲来贵州黑科技辅助软件,久久互娱科技(辅助)竟然是有软件(哔哩哔哩)1、用户打开应用后...