• 聊聊logback的LevelFilter


    本文主要研究一下logback的LevelFilter

    AbstractMatcherFilter

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

    public abstract class AbstractMatcherFilter extends Filter {
    
        protected FilterReply onMatch = FilterReply.NEUTRAL;
        protected FilterReply onMismatch = FilterReply.NEUTRAL;
    
        final public void setOnMatch(FilterReply reply) {
            this.onMatch = reply;
        }
    
        final public void setOnMismatch(FilterReply reply) {
            this.onMismatch = reply;
        }
    
        final public FilterReply getOnMatch() {
            return onMatch;
        }
    
        final public FilterReply getOnMismatch() {
            return onMismatch;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    AbstractMatcherFilter继承了Filter,它定义了onMatch及onMismatch属性

    LevelFilter

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

    public class LevelFilter extends AbstractMatcherFilter {
    
        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();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    LevelFilter继承了AbstractMatcherFilter,其decide判断event的level等级是否与配置的level一致,一致则返回onMatch,否则返回onMismatch

    示例

        
        	
                ERROR
                ACCEPT
                DENY
            
            
                ${CONSOLE_LOG_PATTERN}
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里CONSOLE的appender定义了LevelFilter,当level为ERROR级别时ACCEPT,否则DENY

    小结

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

  • 相关阅读:
    Unity 顶点vertices,uv,与图片贴图,与mesh
    【matplotlib 实战】--柱状图
    Docker方式创建MySQL8的MGR集群
    SpringMVC (3)—拦截器
    Halcon xld镜像操作
    ubuntu安装electerm
    Spring之bean对象
    泰安ITSS认证流程,认证条件
    【esp32c3配置arduino IDE教程】
    Excel 自动提取某一列不重复值
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134521609