SpringBoot+log4j2.xml使用application.yml属性值问题

 更新时间:2023年12月11日 08:40:56   作者:extjava  
这篇文章主要介绍了SpringBoot+log4j2.xml使用application.yml属性值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

项目中有个需求,需要log4j2.xml加载application.yml的属性,折腾了半天,贴代码吧:

1.自定义启动监听ApplicationStartedEventListener

代码中标红的就是从yml中读取的属性,然后通过MDC设置到log4j2的上下文

package com.wm.dcm.utils;

import org.slf4j.MDC;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

/**
 * @ClassName: MyApplicationStartedEventListener
 * @Description:TODO
 * @author: SUN
 * @date: 2017年9月19日 下午5:51:04
 * 
 */
public class ApplicationStartedEventListener implements GenericApplicationListener {
    
    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
    
    private static Class<?>[] EVENT_TYPES = { ApplicationStartingEvent.class,
            ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
            ContextClosedEvent.class, ApplicationFailedEvent.class };

    private static Class<?>[] SOURCE_TYPES = { SpringApplication.class,
            ApplicationContext.class };

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {

            ConfigurableEnvironment envi = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
            MutablePropertySources mps = envi.getPropertySources();

            PropertySource<?> ps = mps.get("applicationConfigurationProperties");

            if (ps != null && ps.containsProperty("spring.kafka.bootstrap-servers")) {
                String kafkaUrl = (String) ps.getProperty("spring.kafka.bootstrap-servers");
                //System.out.println(kafkaUrl);
                MDC.put("host", kafkaUrl);
            }
            
            if (ps != null && ps.containsProperty("logging.file")) {
                String fileName = (String) ps.getProperty("logging.file");
                //System.out.println(kafkaUrl);
                MDC.put("fileName", fileName);
            }

        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.core.Ordered#getOrder()
     */
    @Override
    public int getOrder() {
        // TODO Auto-generated method stub
        return DEFAULT_ORDER;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.context.event.GenericApplicationListener#
     * supportsEventType(org.springframework.core.ResolvableType)
     */
    @Override
    public boolean supportsEventType(ResolvableType resolvableType) {
        return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES);
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        return isAssignableFrom(sourceType, SOURCE_TYPES);
    }

    private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) {
        if (type != null) {
            for (Class<?> supportedType : supportedTypes) {
                if (supportedType.isAssignableFrom(type)) {
                    return true;
                }
            }
        }
        return false;
    }
}

2.在Application启动类中添加

自定义的启动监听ApplicationStartedEventListener

package com.wm.dcm.db;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.wm.dcm.constant.LogTypeAndLevel;
import com.wm.dcm.utils.ApplicationStartedEventListener;

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = { "com.wm.dcm" })
@EnableKafka
@EnableAsync
public class DBApplication {
private String bootstrap;

    public String getBootstrap() {
        return bootstrap;
    }

    public void setBootstrap(String bootstrap) {
        this.bootstrap = bootstrap;
    }

    /**
     * The main method.
     *
     * @param args
     *            the arguments
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {



        // System.out.println(env);

        SpringApplication app = new SpringApplication(DBApplication.class);

        Set<ApplicationListener<?>> ls = app.getListeners();

        ApplicationStartedEventListener asel = new ApplicationStartedEventListener();

        app.addListeners(asel);
        
        app.run(args);


    }

}

3.在log4j2.xml中使用MDC定义的属性

标红的就是使用方式

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    
        <Appenders>
            <RollingFile name="RollingFile" fileName="logs/${ctx:fileName}"
                filePattern="logs/$${date:yyyy-MM}/${ctx:fileName}-%d{MM-dd-yyyy}-%i.log.gz"
                immediateFlush="true" append="true">
                <PatternLayout charset="UTF-8" pattern="[%-5p] %d[%t]  [%c] - %m%n" />

                <SizeBasedTriggeringPolicy size="50MB" />
                <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
                <DefaultRolloverStrategy max="20" />
            </RollingFile>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout charset="UTF-8" pattern="[%-5p] %d[%t]  [%c] - %m%n" />
            </Console>
            <Kafka name="Kafka" topic="wmdcm_log">
                <JSONLayout complete="false" compact="true" locationInfo="true" />
                
                <Property name="bootstrap.servers" value="${ctx:host}"/>
            </Kafka>
        </Appenders>


    <Loggers>
        <!-- root loggers <AppenderRef ref="Console" /> -->
        <Root level="info" includeLocation="true">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
            <AppenderRef ref="Kafka" />
        </Root>
        <Logger name="org.apache.kafka" level="ERROR" />
        <Logger name="org.springframework.kafka" level="ERROR" />
    </Loggers>



</Configuration>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring Boot自定义注解从入门到实战指南

    Spring Boot自定义注解从入门到实战指南

    本文介绍了SpringBoot中自定义注解的设计、实现与应用技巧,涵盖了注解基础、创建自定义注解、注解处理器实现方案、高级注解特性、性能优化与最佳实践、实战案例、测试策略、常见问题与解决方案以及总结与最佳实践,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • SpringBoot实现图片识别文字的四种方式小结

    SpringBoot实现图片识别文字的四种方式小结

    本文主要介绍了SpringBoot实现图片识别文字的四种方式,包括Tess4J,百度智能云,阿里云,腾讯云这四种,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • Java的关键字与保留字小结

    Java的关键字与保留字小结

    Java 保留字列表 (依字母排序 共14组) : Java保留字是指现有Java版本尚未使用 但以后版本可能会作为关键字使用
    2012-10-10
  • SpringBoot结合Redis配置工具类实现动态切换库

    SpringBoot结合Redis配置工具类实现动态切换库

    本文主要介绍了SpringBoot结合Redis配置工具类实现动态切换库,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Java MD5加密(实例讲解)

    Java MD5加密(实例讲解)

    下面小编就为大家带来一篇Java MD5加密(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Nacos(SpringBoot)配置加载及刷新方式

    Nacos(SpringBoot)配置加载及刷新方式

    文章主要介绍了NacosConfigAutoConfiguration的配置加载及刷新过程,包括NacosConfigBeanDefinitionRegistrar的注册、NacosPropertySource的处理、自动刷新机制以及NacosValueAnnotationBeanPostProcessor的实现
    2024-12-12
  • Spring与MyBatis集成 AOP整合PageHelper插件的操作过程

    Spring与MyBatis集成 AOP整合PageHelper插件的操作过程

    Spring与MyBatis集成的主要目的是为了提供更强大的数据访问和事务管理能力,以及简化配置和提高开发效率,这篇文章主要介绍了Spring与MyBatis集成AOP整合PageHelper插件,需要的朋友可以参考下
    2023-08-08
  • 探讨Java中函数是值传递还是引用传递问题

    探讨Java中函数是值传递还是引用传递问题

    这篇文章主要介绍了探讨Java中函数是值传递还是引用传递问题,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • 一文详解Java如何系统地避免空指针问题

    一文详解Java如何系统地避免空指针问题

    新手Java开发总是经常空指针检查,甚至某些老手也会犯这样的问题,所以这篇文章小编就带大家一起来看看如何系统地避免空指针问题,希望对大家有所帮助
    2024-01-01
  • Java实现HttpGet请求传body参数

    Java实现HttpGet请求传body参数

    这篇文章主要为大家详细介绍了Java实现HttpGet请求传body参数的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02

最新评论