log4j2.xml 使用 application.yml 配置的属性

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

 

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

LoggingListener.java

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

复制代码
 1 import org.apache.commons.lang3.StringUtils;
 2 import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
 3 import org.springframework.boot.context.logging.LoggingApplicationListener;
 4 import org.springframework.context.ApplicationEvent;
 5 import org.springframework.context.ApplicationListener;
 6 import org.springframework.core.Ordered;
 7 import org.springframework.core.env.ConfigurableEnvironment;
 8 import org.springframework.stereotype.Component;
 9  
10 /**
11  * 读取yml配置传递到log4jXml中
12  *
13  * @author Clay
14  */
15 @Component
16 public class LoggingListener implements ApplicationListener<ApplicationEvent>, Ordered {
17     /**
18      * 提供给日志文件读取配置的key,使用时需要在前面加上 sys:
19      */
20     private final static String LOG_PATH = "log.path";
21  
22     /**
23      * spring 内部设置的日志文件的配置key
24      */
25     private final static String SPRING_LOG_PATH_PROP = "spring.log-file-path";
26  
27     @Override
28     public void onApplicationEvent(ApplicationEvent applicationEvent) {
29  
30         if (applicationEvent instanceof ApplicationEnvironmentPreparedEvent) {
31             ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) applicationEvent).getEnvironment();
32             String filePath = environment.getProperty(SPRING_LOG_PATH_PROP);
33             if (StringUtils.isNotEmpty(filePath)) {
34                 System.err.println("=================" + filePath);
35                 System.setProperty(LOG_PATH, filePath);
36             }
37         }
38     }
39  
40     @Override
41     public int getOrder() {
42         // 当前监听器的启动顺序需要在日志配置监听器的前面,所以此处减 1
43         return LoggingApplicationListener.DEFAULT_ORDER - 1;
44     }
45  
46 }
复制代码

application.yml

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

log4j2.xml

<Property name="log-path">${sys:log.path}</Property>

Application.java

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

内置Tomcat

复制代码
 1 public class Application {
 2  
 3     public static void main(String[] args) {
 4         SpringApplication application = new SpringApplication(Application.class);
 5         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性
 6         application.addListeners(new LoggingListener());
 7         application.run(args);
 8     }
 9  
10 }
复制代码

外置Tomcat

复制代码
 1 public class TomcatApplication extends SpringBootServletInitializer {
 2  
 3     @Override
 4     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 5         // 添加 日志监听器,使 log4j2-spring.xml 可以间接读取到配置文件的属性
 6         builder.application().addListeners(new LoggingListener());
 7         return builder.sources(Application.class);
 8     }
 9  
10 }
复制代码

 

posted @   Boblim  阅读(1295)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示