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

TraceIdPatternLogbackLayout日志攔截源碼解析

 更新時(shí)間:2023年11月22日 11:09:41   作者:codecraft  
這篇文章主要為大家介紹了TraceIdPatternLogbackLayout日志攔截源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

本文主要研究一下TraceIdPatternLogbackLayout

TraceIdPatternLogbackLayout

org/apache/skywalking/apm/toolkit/log/logback/v1/x/TraceIdPatternLogbackLayout.class

public class TraceIdPatternLogbackLayout extends PatternLayout {
    public TraceIdPatternLogbackLayout() {
    }

    static {
        defaultConverterMap.put("tid", LogbackPatternConverter.class.getName());
    }
}
skywalking的TraceIdPatternLogbackLayout繼承了PatternLayout,其static方法往defaultConverterMap添加了tid,value為LogbackPatternConverter.class.getName()

PatternLayout

ch/qos/logback/classic/PatternLayout.java

public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {
    public static final Map<String, String> defaultConverterMap = new HashMap<String, String>();
    public static final String HEADER_PREFIX = "#logback.classic pattern: ";
    static {
        defaultConverterMap.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
        defaultConverterMap.put("d", DateConverter.class.getName());
        defaultConverterMap.put("date", DateConverter.class.getName());
        defaultConverterMap.put("r", RelativeTimeConverter.class.getName());
        defaultConverterMap.put("relative", RelativeTimeConverter.class.getName());
        defaultConverterMap.put("level", LevelConverter.class.getName());
        defaultConverterMap.put("le", LevelConverter.class.getName());
        defaultConverterMap.put("p", LevelConverter.class.getName());
        defaultConverterMap.put("t", ThreadConverter.class.getName());
        defaultConverterMap.put("thread", ThreadConverter.class.getName());
        defaultConverterMap.put("lo", LoggerConverter.class.getName());
        defaultConverterMap.put("logger", LoggerConverter.class.getName());
        defaultConverterMap.put("c", LoggerConverter.class.getName());
        defaultConverterMap.put("m", MessageConverter.class.getName());
        defaultConverterMap.put("msg", MessageConverter.class.getName());
        defaultConverterMap.put("message", MessageConverter.class.getName());
        defaultConverterMap.put("C", ClassOfCallerConverter.class.getName());
        defaultConverterMap.put("class", ClassOfCallerConverter.class.getName());
        defaultConverterMap.put("M", MethodOfCallerConverter.class.getName());
        defaultConverterMap.put("method", MethodOfCallerConverter.class.getName());
        defaultConverterMap.put("L", LineOfCallerConverter.class.getName());
        defaultConverterMap.put("line", LineOfCallerConverter.class.getName());
        defaultConverterMap.put("F", FileOfCallerConverter.class.getName());
        defaultConverterMap.put("file", FileOfCallerConverter.class.getName());
        defaultConverterMap.put("X", MDCConverter.class.getName());
        defaultConverterMap.put("mdc", MDCConverter.class.getName());
        defaultConverterMap.put("ex", ThrowableProxyConverter.class.getName());
        defaultConverterMap.put("exception", ThrowableProxyConverter.class.getName());
        defaultConverterMap.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());
        defaultConverterMap.put("rootException", RootCauseFirstThrowableProxyConverter.class.getName());
        defaultConverterMap.put("throwable", ThrowableProxyConverter.class.getName());
        defaultConverterMap.put("xEx", ExtendedThrowableProxyConverter.class.getName());
        defaultConverterMap.put("xException", ExtendedThrowableProxyConverter.class.getName());
        defaultConverterMap.put("xThrowable", ExtendedThrowableProxyConverter.class.getName());
        defaultConverterMap.put("nopex", NopThrowableInformationConverter.class.getName());
        defaultConverterMap.put("nopexception", NopThrowableInformationConverter.class.getName());
        defaultConverterMap.put("cn", ContextNameConverter.class.getName());
        defaultConverterMap.put("contextName", ContextNameConverter.class.getName());
        defaultConverterMap.put("caller", CallerDataConverter.class.getName());
        defaultConverterMap.put("marker", MarkerConverter.class.getName());
        defaultConverterMap.put("property", PropertyConverter.class.getName());
        defaultConverterMap.put("n", LineSeparatorConverter.class.getName());
        defaultConverterMap.put("black", BlackCompositeConverter.class.getName());
        defaultConverterMap.put("red", RedCompositeConverter.class.getName());
        defaultConverterMap.put("green", GreenCompositeConverter.class.getName());
        defaultConverterMap.put("yellow", YellowCompositeConverter.class.getName());
        defaultConverterMap.put("blue", BlueCompositeConverter.class.getName());
        defaultConverterMap.put("magenta", MagentaCompositeConverter.class.getName());
        defaultConverterMap.put("cyan", CyanCompositeConverter.class.getName());
        defaultConverterMap.put("white", WhiteCompositeConverter.class.getName());
        defaultConverterMap.put("gray", GrayCompositeConverter.class.getName());
        defaultConverterMap.put("boldRed", BoldRedCompositeConverter.class.getName());
        defaultConverterMap.put("boldGreen", BoldGreenCompositeConverter.class.getName());
        defaultConverterMap.put("boldYellow", BoldYellowCompositeConverter.class.getName());
        defaultConverterMap.put("boldBlue", BoldBlueCompositeConverter.class.getName());
        defaultConverterMap.put("boldMagenta", BoldMagentaCompositeConverter.class.getName());
        defaultConverterMap.put("boldCyan", BoldCyanCompositeConverter.class.getName());
        defaultConverterMap.put("boldWhite", BoldWhiteCompositeConverter.class.getName());
        defaultConverterMap.put("highlight", HighlightingCompositeConverter.class.getName());
        defaultConverterMap.put("lsn", LocalSequenceNumberConverter.class.getName());
    }
    public PatternLayout() {
        this.postCompileProcessor = new EnsureExceptionHandling();
    }
    public Map<String, String> getDefaultConverterMap() {
        return defaultConverterMap;
    }
    public String doLayout(ILoggingEvent event) {
        if (!isStarted()) {
            return CoreConstants.EMPTY_STRING;
        }
        return writeLoopOnConverters(event);
    }
    @Override
    protected String getPresentationHeaderPrefix() {
        return HEADER_PREFIX;
    }
}
PatternLayout繼承了PatternLayoutBase,其static方法往defaultConverterMap添加了一系列的convert

PatternLayoutBase

ch/qos/logback/core/pattern/PatternLayoutBase.java

abstract public class PatternLayoutBase<E> extends LayoutBase<E> {
    static final int INTIAL_STRING_BUILDER_SIZE = 256;
    Converter<E> head;
    String pattern;
    protected PostCompileProcessor<E> postCompileProcessor;
    Map<String, String> instanceConverterMap = new HashMap<String, String>();
    protected boolean outputPatternAsHeader = false;
    //......
    /**
     * Concrete implementations of this class are responsible for elaborating the
     * mapping between pattern words and converters.
     * 
     * @return A map associating pattern words to the names of converter classes
     */
    abstract public Map<String, String> getDefaultConverterMap();
    /**
     * Returns a map where the default converter map is merged with the map
     * contained in the context.
     */
    public Map<String, String> getEffectiveConverterMap() {
        Map<String, String> effectiveMap = new HashMap<String, String>();
        // add the least specific map fist
        Map<String, String> defaultMap = getDefaultConverterMap();
        if (defaultMap != null) {
            effectiveMap.putAll(defaultMap);
        }
        // contextMap is more specific than the default map
        Context context = getContext();
        if (context != null) {
            @SuppressWarnings("unchecked")
            Map<String, String> contextMap = (Map<String, String>) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
            if (contextMap != null) {
                effectiveMap.putAll(contextMap);
            }
        }
        // set the most specific map last
        effectiveMap.putAll(instanceConverterMap);
        return effectiveMap;
    }    
}
PatternLayoutBase繼承了LayoutBase,它定義了抽象類getDefaultConverterMap,而getEffectiveConverterMap方法返回的effectiveMap則包含了defaultConverterMap,它還包含了context中key為CoreConstants.PATTERN_RULE_REGISTRY注冊的,以及本身定義的instanceConverterMap

LogbackPatternConverter

org/apache/skywalking/apm/toolkit/log/logback/v1/x/LogbackPatternConverter.class

public class LogbackPatternConverter extends ClassicConverter {
    public LogbackPatternConverter() {
    }
    public String convert(ILoggingEvent iLoggingEvent) {
        return "TID: N/A";
    }
}
LogbackPatternConverter繼承了ClassicConverter,其convert方法返回TID: N/A

ClassicConverter

ch/qos/logback/classic/pattern/ClassicConverter.java

abstract public class ClassicConverter extends DynamicConverter&lt;ILoggingEvent&gt; {

}
ClassicConverter繼承了DynamicConverter,其泛型為ILoggingEvent

DynamicConverter

ch/qos/logback/core/pattern/DynamicConverter.java

abstract public class DynamicConverter<E> extends FormattingConverter<E> implements LifeCycle, ContextAware {
    ContextAwareBase cab = new ContextAwareBase(this);
    // Contains a list of option Strings.
    private List<String> optionList;
    /**
     * Is this component active?
     */
    protected boolean started = false;
    /**
     * Components that depend on options passed during configuration can override
     * this method in order to make appropriate use of those options. For simpler
     * components, the trivial implementation found in this abstract class will be
     * sufficient.
     */
    public void start() {
        started = true;
    }
    public void stop() {
        started = false;
    }
    public boolean isStarted() {
        return started;
    }
    public void setOptionList(List<String> optionList) {
        this.optionList = optionList;
    }
    /**
     * Return the first option passed to this component. The returned value may be
     * null if there are no options.
     * 
     * @return First option, may be null.
     */
    public String getFirstOption() {
        if (optionList == null || optionList.size() == 0) {
            return null;
        } else {
            return optionList.get(0);
        }
    }
    protected List<String> getOptionList() {
        return optionList;
    }
    public void setContext(Context context) {
        cab.setContext(context);
    }
    public Context getContext() {
        return cab.getContext();
    }
    public void addStatus(Status status) {
        cab.addStatus(status);
    }
    public void addInfo(String msg) {
        cab.addInfo(msg);
    }
    public void addInfo(String msg, Throwable ex) {
        cab.addInfo(msg, ex);
    }
    public void addWarn(String msg) {
        cab.addWarn(msg);
    }
    public void addWarn(String msg, Throwable ex) {
        cab.addWarn(msg, ex);
    }
    public void addError(String msg) {
        cab.addError(msg);
    }
    public void addError(String msg, Throwable ex) {
        cab.addError(msg, ex);
    }
}
DynamicConverter繼承了FormattingConverter,聲明實(shí)現(xiàn)LifeCycle, ContextAware接口

FormattingConverter

ch/qos/logback/core/pattern/FormattingConverter.java

abstract public class FormattingConverter<E> extends Converter<E> {
    static final int INITIAL_BUF_SIZE = 256;
    static final int MAX_CAPACITY = 1024;
    FormatInfo formattingInfo;
    final public FormatInfo getFormattingInfo() {
        return formattingInfo;
    }
    final public void setFormattingInfo(FormatInfo formattingInfo) {
        if (this.formattingInfo != null) {
            throw new IllegalStateException("FormattingInfo has been already set");
        }
        this.formattingInfo = formattingInfo;
    }
    @Override
    final public void write(StringBuilder buf, E event) {
        String s = convert(event);
        if (formattingInfo == null) {
            buf.append(s);
            return;
        }
        int min = formattingInfo.getMin();
        int max = formattingInfo.getMax();
        if (s == null) {
            if (0 < min)
                SpacePadder.spacePad(buf, min);
            return;
        }
        int len = s.length();
        if (len > max) {
            if (formattingInfo.isLeftTruncate()) {
                buf.append(s.substring(len - max));
            } else {
                buf.append(s.substring(0, max));
            }
        } else if (len < min) {
            if (formattingInfo.isLeftPad()) {
                SpacePadder.leftPad(buf, s, min);
            } else {
                SpacePadder.rightPad(buf, s, min);
            }
        } else {
            buf.append(s);
        }
    }
}
FormattingConverter繼承了Converter,它定義了formattingInfo屬性,其write方法根據(jù)formattingInfo信息進(jìn)行append

示例

<property name="PATTERN_DEFAULT" value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%thread] [%tid] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">     
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                <pattern>${PATTERN_DEFAULT}</pattern>
            </layout>
        </encoder>
    </appender>
這里采用了TraceIdPatternLogbackLayout,并在pattern中使用了tid

小結(jié)

skywalking的apm-toolkit-logback-1.x組件提供了TraceIdPatternLogbackLayout,可以在日志中打印tid。

以上就是TraceIdPatternLogbackLayout的詳細(xì)內(nèi)容,更多關(guān)于TraceIdPatternLogbackLayout的資料請關(guān)注腳本之家其它相關(guān)文章!

以上就是TraceIdPatternLogbackLayout日志攔截源碼解析的詳細(xì)內(nèi)容,更多關(guān)于TraceIdPatternLogbackLayout日志攔截的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java.net.ConnectException異常的正確解決方法(親測有效!)

    java.net.ConnectException異常的正確解決方法(親測有效!)

    java.net.ConnectException異常是與網(wǎng)絡(luò)相關(guān)的最常見的Java異常之一,建立從客戶端應(yīng)用程序到服務(wù)器的TCP連接時(shí),我們可能會(huì)遇到它,這篇文章主要給大家介紹了關(guān)于java.net.ConnectException異常的正確解決方法,需要的朋友可以參考下
    2024-01-01
  • Java的注解原理詳解

    Java的注解原理詳解

    這篇文章主要介紹了Java的注解原理詳解,注解是JDK1.5引入的新特性,包含在java.lang.annotation包中,它是附加在代碼中的一些元信息,將一個(gè)類的外部信息與內(nèi)部成員聯(lián)系起來,在編 譯、運(yùn)行時(shí)進(jìn)行解析和使用,需要的朋友可以參考下
    2023-10-10
  • Java開發(fā)之Lombok指南

    Java開發(fā)之Lombok指南

    Lombok是一款Java開發(fā)插件,使得Java開發(fā)者可以通過其定義的一些注解來消除業(yè)務(wù)工程中冗長和繁瑣的代碼,它能夠在編譯源代碼期間自動(dòng)幫我們生成這些方法,并沒有如反射那樣降低程序的性能。下面我們來詳細(xì)了解一下吧
    2019-06-06
  • MyBatis中正則使用foreach拼接字符串

    MyBatis中正則使用foreach拼接字符串

    這篇文章主要介紹了MyBatis中正則使用foreach拼接字符串,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 解決idea無法導(dǎo)入識別本地類的問題

    解決idea無法導(dǎo)入識別本地類的問題

    今天做實(shí)驗(yàn)不知道按了哪里不能導(dǎo)入識別本地的類,只有jar包的類,百度搜索也沒有找到合理的解決方案,經(jīng)過朋友援助問題根源找到,下面小編把解決方法分享給大家,需要的朋友參考下吧
    2021-08-08
  • 你真的會(huì)使用Java的方法引用嗎

    你真的會(huì)使用Java的方法引用嗎

    這篇文章主要給大家介紹了關(guān)于Java方法引用的相關(guān)資料,方法引用是Java8的新特性,方法引用其實(shí)也離不開Lambda表達(dá)式,本文通過示例代碼介紹的很詳細(xì),需要的朋友可以參考下
    2021-08-08
  • java工具類實(shí)現(xiàn)文件壓縮zip以及解壓縮功能

    java工具類實(shí)現(xiàn)文件壓縮zip以及解壓縮功能

    這篇文章主要給大家介紹了關(guān)于java工具類實(shí)現(xiàn)文件壓縮zip以及解壓縮功能的相關(guān)資料,文中主要使用使用的是hutool工具類,Hutool是一個(gè)Java工具類庫,由國內(nèi)的程序員loolly開發(fā),目的是提供一些方便、快捷、實(shí)用的工具類和工具方法,需要的朋友可以參考下
    2024-02-02
  • swagger注解@ApiModelProperty失效情況的解決

    swagger注解@ApiModelProperty失效情況的解決

    這篇文章主要介紹了swagger注解@ApiModelProperty失效情況的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹

    帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹

    這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Maven Repository倉庫的具體使用

    Maven Repository倉庫的具體使用

    本文主要介紹了Maven Repository倉庫的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05

最新評論

宜丰县| 玉林市| 神池县| 赣州市| 万盛区| 民丰县| 嵩明县| 江门市| 襄垣县| 城口县| 扎兰屯市| 襄城县| 定西市| 黄冈市| 乌兰察布市| 九龙坡区| 张北县| 安岳县| 鸡泽县| 麻阳| 沈阳市| 新巴尔虎右旗| 会昌县| 永丰县| 长海县| 丰镇市| 唐海县| 共和县| 张北县| 呈贡县| 达拉特旗| 东光县| 敖汉旗| 武定县| 临夏市| 安宁市| 文登市| 清丰县| 奉新县| 隆子县| 威信县|