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

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

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

LoggingListener.java

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

java 复制代码
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<ApplicationEvent>, 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

yml 复制代码
spring: 
  log-file-path: "F:/logs/"

log4j2.xml

xml 复制代码
<Property name="log-path">${sys:log.path}</Property>

Application.java

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

内置Tomcat

java 复制代码
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

java 复制代码
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

相关推荐
Flying_Fish_roe7 小时前
Spring Boot-Bean注入问题
spring boot·后端·log4j
Dingdangr12 小时前
AndroidManifest.xml文件的重要信息
xml
SelectDB技术团队16 小时前
查询性能提升 10 倍、存储空间节省 65%,Apache Doris 半结构化数据分析方案及典型场景
数据结构·数据仓库·elasticsearch·log4j·json
OEC小胖胖17 小时前
MyBatis 如何将 Mapper 接口与其 XML 映射文件关联:深入原理与实现
xml·java·后端·mybatis·web
_Shirley17 小时前
android.view.InflateException: Binary XML file line #7: Error inflating class
android·xml·java·ide·kotlin·android studio
陈逸轩*^_^*18 小时前
xml重点笔记(尚学堂 3h)
xml·java·笔记·java-ee·intellij-idea
codelife32118 小时前
记录word转xml文件踩坑
xml·word
蔚一2 天前
Java设计模式—面向对象设计原则(六) ----->合成复用原则(CRP) (完整详解,附有代码+案例)
xml·java·开发语言·设计模式·intellij-idea·合成复用原则
_长银3 天前
好用的XML解析库——fast-xml-parser
xml
箬敏伊儿3 天前
Spring Boot 项目的 pom.xml 中,groupId、artifactId 等信息要如何定义?——定义规则及案例
xml·java·spring boot·后端·spring