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

相关内容

热门资讯

黑科技计算!wepoke辅助挂... 黑科技计算!wepoke辅助挂,wepooke系统规律,攻略教程(原先真的有挂);玩家必备必赢加哟《...
wepoke黑科技ai(Wep... wepoke黑科技ai(WepokE)wepoke软件还可以用吗(黑科技)德州教程(真是是有挂);一...
黑科技私人局!微扑克全自动机器... 黑科技私人局!微扑克全自动机器人,wpk微扑克真的有辅助插件吗,线上教程(最初真的是有挂);软件透明...
wepower有外挂(wepo... wepower有外挂(wepoker)we辅助poker德之星(黑科技)德州论坛(一贯存在有挂);1...
黑科技科技!wepoke调控参... 黑科技科技!wepoke调控参数,wepoke能赢嘛,2025新版技巧(最初真的是有挂);科技安装教...
wepokeai代打逻辑(we... wepokeai代打逻辑(wePOke)wepoke辅助透视是不是真的(黑科技)AA德州教程(切实真...
黑科技讲解!wepower辅助... 黑科技讲解!wepower辅助器,wepoke有规律吗,靠谱教程(往昔是有挂);支持2-10人实时对...
aapoker透明(德州aap... aapoker透明(德州aapoker俱乐部)aa扑克有挂吗(辅助挂)解密教程(好像是有挂);亲,关...
黑科技神器!德州之星app有外... 黑科技神器!德州之星app有外挂吗,德州辅助软件开发定制,大神讲解(好像真的有挂);科技安装教程;1...
aapoker系统机制(aAp... aapoker系统机制(aApoker)德州aapoker俱乐部正确打法(辅助挂)2025新版教程(...