TraceIdPatternLogbackLayout日志拦截源码解析

 更新时间:2023年11月22日 11:09:41   作者:codecraft  
这篇文章主要为大家介绍了TraceIdPatternLogbackLayout日志拦截源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下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,声明实现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方法根据formattingInfo信息进行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

小结

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

以上就是TraceIdPatternLogbackLayout的详细内容,更多关于TraceIdPatternLogbackLayout的资料请关注脚本之家其它相关文章!

以上就是TraceIdPatternLogbackLayout日志拦截源码解析的详细内容,更多关于TraceIdPatternLogbackLayout日志拦截的资料请关注脚本之家其它相关文章!

相关文章

  • Java函数式编程之通过行为参数化传递代码

    Java函数式编程之通过行为参数化传递代码

    行为参数化就是可以帮助你处理频繁变更的需求的一种软件开发模式,这篇文章将给大家详细的介绍一下Java函数式编程之行为参数化传递代码,感兴趣的同学可以参考阅读下
    2023-08-08
  • java实现支付宝退款功能

    java实现支付宝退款功能

    这篇文章主要为大家详细 介绍了java实现支付宝退款功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • java实现延迟/超时/定时问题

    java实现延迟/超时/定时问题

    这篇文章主要介绍了java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • 使用Netty进行编解码的操作过程详解

    使用Netty进行编解码的操作过程详解

    这篇文章主要介绍了使用Netty进行编解码的操作过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 解决idea services窗口不见的一种特殊情况(小白采坑系列)

    解决idea services窗口不见的一种特殊情况(小白采坑系列)

    这篇文章主要介绍了解决idea services窗口不见的一种特殊情况(小白采坑系列),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 使用spring security BCryptPasswordEncoder接入系统

    使用spring security BCryptPasswordEncoder接入系统

    这篇文章主要介绍了使用spring security BCryptPasswordEncoder接入系统方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • springBoot整合redis做缓存具体操作步骤

    springBoot整合redis做缓存具体操作步骤

    缓存主要是将数据存在计算机的内存当中,以便于在使用的时候是可以实现快速读取使用,它的快也是相对于硬盘读取而言,这篇文章主要给大家介绍了关于springBoot整合redis做缓存的具体操作步骤,需要的朋友可以参考下
    2024-04-04
  • Java实现画图 给图片底部添加文字标题

    Java实现画图 给图片底部添加文字标题

    这篇文章主要介绍了Java实现画图 给图片底部添加文字标题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java高级特性(基础)

    Java高级特性(基础)

    这篇文章主要介绍了Java高级特性(基础),需要的朋友可以参考下
    2017-04-04
  • 浅谈java实现mongoDB的多条件查询

    浅谈java实现mongoDB的多条件查询

    这篇文章主要介绍了java实现mongoDB的多条件查询,具有一定参考价值,需要的朋友可以参考下。
    2017-09-09

最新评论