最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot+log4j2.xml使用application.yml屬性值問題

 更新時(shí)間:2023年12月11日 08:40:56   作者:extjava  
這篇文章主要介紹了SpringBoot+log4j2.xml使用application.yml屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

項(xiàng)目中有個(gè)需求,需要log4j2.xml加載application.yml的屬性,折騰了半天,貼代碼吧:

1.自定義啟動(dòng)監(jiān)聽ApplicationStartedEventListener

代碼中標(biāo)紅的就是從yml中讀取的屬性,然后通過MDC設(shè)置到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啟動(dòng)類中添加

自定義的啟動(dòng)監(jiān)聽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定義的屬性

標(biāo)紅的就是使用方式

<?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屬性如不設(shè)置,則默認(rèn)為最多同一文件夾下7個(gè)文件,這里設(shè)置了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>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot自定義注解從入門到實(shí)戰(zhàn)指南

    Spring Boot自定義注解從入門到實(shí)戰(zhàn)指南

    本文介紹了SpringBoot中自定義注解的設(shè)計(jì)、實(shí)現(xiàn)與應(yīng)用技巧,涵蓋了注解基礎(chǔ)、創(chuàng)建自定義注解、注解處理器實(shí)現(xiàn)方案、高級(jí)注解特性、性能優(yōu)化與最佳實(shí)踐、實(shí)戰(zhàn)案例、測(cè)試策略、常見問題與解決方案以及總結(jié)與最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式小結(jié)

    SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式小結(jié)

    本文主要介紹了SpringBoot實(shí)現(xiàn)圖片識(shí)別文字的四種方式,包括Tess4J,百度智能云,阿里云,騰訊云這四種,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Java的關(guān)鍵字與保留字小結(jié)

    Java的關(guān)鍵字與保留字小結(jié)

    Java 保留字列表 (依字母排序 共14組) : Java保留字是指現(xiàn)有Java版本尚未使用 但以后版本可能會(huì)作為關(guān)鍵字使用
    2012-10-10
  • SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫

    SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫

    本文主要介紹了SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java MD5加密(實(shí)例講解)

    Java MD5加密(實(shí)例講解)

    下面小編就為大家?guī)硪黄狫ava MD5加密(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Nacos(SpringBoot)配置加載及刷新方式

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

    文章主要介紹了NacosConfigAutoConfiguration的配置加載及刷新過程,包括NacosConfigBeanDefinitionRegistrar的注冊(cè)、NacosPropertySource的處理、自動(dòng)刷新機(jī)制以及NacosValueAnnotationBeanPostProcessor的實(shí)現(xiàn)
    2024-12-12
  • Spring與MyBatis集成?AOP整合PageHelper插件的操作過程

    Spring與MyBatis集成?AOP整合PageHelper插件的操作過程

    Spring與MyBatis集成的主要目的是為了提供更強(qiáng)大的數(shù)據(jù)訪問和事務(wù)管理能力,以及簡(jiǎn)化配置和提高開發(fā)效率,這篇文章主要介紹了Spring與MyBatis集成AOP整合PageHelper插件,需要的朋友可以參考下
    2023-08-08
  • 探討Java中函數(shù)是值傳遞還是引用傳遞問題

    探討Java中函數(shù)是值傳遞還是引用傳遞問題

    這篇文章主要介紹了探討Java中函數(shù)是值傳遞還是引用傳遞問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • 一文詳解Java如何系統(tǒng)地避免空指針問題

    一文詳解Java如何系統(tǒng)地避免空指針問題

    新手Java開發(fā)總是經(jīng)??罩羔槞z查,甚至某些老手也會(huì)犯這樣的問題,所以這篇文章小編就帶大家一起來看看如何系統(tǒng)地避免空指針問題,希望對(duì)大家有所幫助
    2024-01-01
  • Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)

    Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02

最新評(píng)論

兰溪市| 株洲县| 桃江县| 澄迈县| 汤原县| 莱西市| 梅河口市| 宜丰县| 万宁市| 涡阳县| 肃北| 绥江县| 同心县| 米脂县| 永德县| 舞钢市| 武功县| 临西县| 扬州市| 沙田区| 蕲春县| 东乡| 郸城县| 永寿县| 张家界市| 砀山县| 苍南县| 赣州市| 宜君县| 无棣县| 浦江县| 凤凰县| 逊克县| 金昌市| 太原市| 韶关市| 驻马店市| 濉溪县| 嘉禾县| 肇庆市| 浠水县|