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

相关内容

热门资讯

第三方技巧!hhpoker软件... 您好,hhpoker可以控制吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
最终!德普之星透视软件免费入口... 最终!德普之星透视软件免费入口官网,aapoker透视脚本,切实存在有挂(今日头条)1、下载好德普之...
日前!模拟器打开hhpoker... 日前!模拟器打开hhpoker,德普辅助软件,总是确实有挂(有挂解惑)1、进入到模拟器打开hhpok...
2026版总结!werplan... 2026版总结!werplan免费挂下载,德州圈脚本,好像真的有挂(有挂技巧)一、werplan免费...
反观!wpk真吗,hhpoke... 反观!wpk真吗,hhpoker破解工具,都是真的有挂(证实有挂)运hhpoker破解工具辅助工具,...
在玩家背景下!hhpoker透... 在玩家背景下!hhpoker透视脚本下载,hhpoker辅助挂,一直确实有挂(有挂详情)1、让任何用...
黑科技辅助挂!fishpoke... 黑科技辅助挂!fishpoker透视底牌,hhpoker有没有作弊挂,一直真的是有挂(真的有挂)1、...
今天上午!aapoker怎么选... 今天上午!aapoker怎么选牌,wepoker私人辅助器,都是是真的有挂(有挂实锤)1、不需要AI...
据目击者称!wpk德州局透视,... 据目击者称!wpk德州局透视,wpk是真的还是假的,真是存在有挂(有挂规律)1、玩家可以在wpk是真...
第三方技巧!xpoker辅助工... 第三方技巧!xpoker辅助工具,wpk俱乐部怎么作弊,一直是有挂(有挂透明挂)1、上手简单,内置详...