本文主要研究一下logback的MDCFilter

MatchingFilter

ch/qos/logback/classic/turbo/MatchingFilter.java

public abstract class MatchingFilter extends TurboFilter {

    protected FilterReply onMatch = FilterReply.NEUTRAL;
    protected FilterReply onMismatch = FilterReply.NEUTRAL;

    final public void setOnMatch(String action) {
        if ("NEUTRAL".equals(action)) {
            onMatch = FilterReply.NEUTRAL;
        } else if ("ACCEPT".equals(action)) {
            onMatch = FilterReply.ACCEPT;
        } else if ("DENY".equals(action)) {
            onMatch = FilterReply.DENY;
        }
    }

    final public void setOnMismatch(String action) {
        if ("NEUTRAL".equals(action)) {
            onMismatch = FilterReply.NEUTRAL;
        } else if ("ACCEPT".equals(action)) {
            onMismatch = FilterReply.ACCEPT;
        } else if ("DENY".equals(action)) {
            onMismatch = FilterReply.DENY;
        }
    }
}
MatchingFilter继承了TurboFilter,它提供了setOnMatch及setOnMismatch方法,它们在action为NEUTRAL、ACCEPT、DENY时设置对应的onMatch、onMismatch

MDCFilter

ch/qos/logback/classic/turbo/MDCFilter.java

public class MDCFilter extends MatchingFilter {

    String MDCKey;
    String value;

    @Override
    public void start() {
        int errorCount = 0;
        if (value == null) {
            addError("\'value\' parameter is mandatory. Cannot start.");
            errorCount++;
        }
        if (MDCKey == null) {
            addError("\'MDCKey\' parameter is mandatory. Cannot start.");
            errorCount++;
        }

        if (errorCount == 0)
            this.start = true;
    }

    @Override
    public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {

        if (!isStarted()) {
            return FilterReply.NEUTRAL;
        }

        String value = MDC.get(MDCKey);
        if (this.value.equals(value)) {
            return onMatch;
        }
        return onMismatch;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setMDCKey(String MDCKey) {
        this.MDCKey = MDCKey;
    }

}
MDCFilter继承了MatchingFilter,其start方法校验MDCKey及value属性是否有值,其decide方法跟从MDC获取指定key的值,然后判断该值域value是否相等,相等则返回onMatch,否则返回onMismatch

示例

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>

  <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
    <MDCKey>username</MDCKey>
    <Value>sebastien</Value>
    <OnMatch>ACCEPT</OnMatch>
  </turboFilter>

  <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
    <Marker>billing</Marker>
    <OnMatch>DENY</OnMatch>
  </turboFilter>

  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%date [%thread] %-5level %logger - %msg%n</Pattern>
    </layout>
  </appender>

  <root level="info">
    <appender-ref ref="console" />
  </root>
</configuration>

小结

logback提供了MDCFilter,它可以根据指定的MDCKey从MDC取值,然后根据配置的value进行判断,然后执行onMatch或者onMismatch来决定是否打印日志。


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...