开发者

logback的LevelFilter日志过滤器源码解读

开发者 https://www.devze.com 2023-11-21 10:18 出处:网络 作者: codecraft
目录序AbstractMatcherFilterLevelFilter示例小结序 本文主要研究一下logback的LevelFilter
目录
  • AbstractMatcherFilter
  • LevelFilter
  • 示例
  • 小结

本文主要研究一下logback的LevelFilter

AbstractMatcherFilter

ch/qos/logback/core/filter/AbstractMatcherFilter.Java

public abstract class AbstractMatcherFilter<E> extends Filter<E> {
    protected FilterReply onMatch = FilterReply.NEUTRAL;
    protected FilterReply onMismatch = FilterReply.NEUTRAL;
    final public void setOnMatch(FilterReplZIquKuJOey rephttp://www.devze.comly) {
        this.onMatch = reply;
    }
    final public pythonvoid setOnMismatch(FilterReply reply) {
        this.onMismatch = reply;
    }
    final public FilterReply getOnMatch() {
        return onMatch;
    }
    final public FilterReply getOnMismatch() {
        return onMismatch;
    }
}
AbstractMatcherFilter继承了Filter,它定义了onMatch及onMismatch属性

LevelFilter

ch/qos/logback/classic/filter/LevelFilter.java

public class LevelandroidFilter extends AbstractMatcherFilter<ILoggingEvent> {
    Level level;
    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (!isStarted()) {
            return FilterReply.NEUTRAL;
        }
        if (event.getLevel().equals(level)) {
            return onMatch;
        } else {
            return onMismatch;
        }
    }
    public void setLevel(Level level) {
        this.level = level;
    }
    public void start() {
        if (this.level != null) {
            super.start();
        }
    }
}
LevelFilter继承了AbstractMatcherFilter,其decide判断event的level等级是否与配置的level一致,一致则返回onMatch,否则返回onMismatch

示例

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
这里CONSOLE的appender定义了LevelFilter,当level为ERROR级别时ACCEPT,否则DENY

小结

logback提供了LevelFilter,可以配置指定的level、onMatch、onMismatch属性,用于设置指定appender的打印级别。

以上就是logback的Le编程客栈velFilter的详细内容,更多关于logback的LevelFilter的资料请关注编程客栈(www.devze.com)其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号