• 聊聊logback的ThresholdFilter


    本文主要研究一下logback的ThresholdFilter

    ThresholdFilter

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

    public class ThresholdFilter extends Filter {
    
        Level level;
    
        @Override
        public FilterReply decide(ILoggingEvent event) {
            if (!isStarted()) {
                return FilterReply.NEUTRAL;
            }
    
            if (event.getLevel().isGreaterOrEqual(level)) {
                return FilterReply.NEUTRAL;
            } else {
                return FilterReply.DENY;
            }
        }
    
        public void setLevel(String level) {
            this.level = Level.toLevel(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

    ThresholdFilter继承了Filter,其decide方法从ILoggingEvent获取level,若该level大于等于指定的level则返回NEUTRAL,否则返回DENY

    isGreaterOrEqual

    ch/qos/logback/classic/Level.java

    public final class Level implements java.io.Serializable {
    
        private static final long serialVersionUID = -814092767334282137L;
    
        public static final int OFF_INT = Integer.MAX_VALUE;
        public static final int ERROR_INT = 40000;
        public static final int WARN_INT = 30000;
        public static final int INFO_INT = 20000;
        public static final int DEBUG_INT = 10000;
        public static final int TRACE_INT = 5000;
        public static final int ALL_INT = Integer.MIN_VALUE;
    
        /**
         * The OFF is used to turn off logging.
         */
        public static final Level OFF = new Level(OFF_INT, "OFF");
    
        /**
         * The ERROR level designates error events which may or not be
         * fatal to the application.
         */
        public static final Level ERROR = new Level(ERROR_INT, "ERROR");
    
        /**
         * The WARN level designates potentially harmful situations.
         */
        public static final Level WARN = new Level(WARN_INT, "WARN");
    
        /**
         * The INFO level designates informational messages highlighting
         * overall progress of the application.
         */
        public static final Level INFO = new Level(INFO_INT, "INFO");
    
        /**
         * The DEBUG level designates informational events of lower
         * importance.
         */
        public static final Level DEBUG = new Level(DEBUG_INT, "DEBUG");
    
        /**
         * The TRACE level designates informational events of very low
         * importance.
         */
        public static final Level TRACE = new Level(TRACE_INT, "TRACE");
    
        /**
         * The ALL is used to turn on all logging.
         */
        public static final Level ALL = new Level(ALL_INT, "ALL");
    
        public final int levelInt;
        public final String levelStr;
    
        /**
         * Instantiate a Level object.
         */
        private Level(int levelInt, String levelStr) {
            this.levelInt = levelInt;
            this.levelStr = levelStr;
        }
    
        /**
         * Returns true if this Level has a higher or equal Level than the
         * Level passed as argument, false otherwise.
         */
        public boolean isGreaterOrEqual(Level r) {
            return levelInt >= r.levelInt;
        }
    
        //......
    }    
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    Level定义了OFF(Integer.MAX_VALUE)、ERROR(40000)、WARN(30000)、INFO(20000)、DEBUG(10000)、TRACE(5000)、ALL(Integer.MIN_VALUE)这几个level,其int值依次减小。isGreaterOrEqual方法则是根据levelInt值来判断。

    示例

    	
    		infoLog.log
    		
    			infoLogs.%d{yyyy-MM-dd}.log
    
    			30
    			3GB
    		
    
    		
    			INFO
    		
    
    		
    			%-4relative [%thread] %-5level %logger{35} - %msg%n
    			
    		
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这里对FILE_INFO添加了ThresholdFilter,只有级别大于等于INFO的才打印

    小结

    logback的ThresholdFilter继承了Filter,其decide方法从ILoggingEvent获取level,若该level大于等于指定的level则返回NEUTRAL,否则返回DENY。Level定义了OFF(Integer.MAX_VALUE)、ERROR(40000)、WARN(30000)、INFO(20000)、DEBUG(10000)、TRACE(5000)、ALL(Integer.MIN_VALUE)这几个level,其int值依次减小。isGreaterOrEqual方法则是根据levelInt值来判断。

  • 相关阅读:
    图的存储结构
    Oracle/PLSQL: Group_ID Function
    cisco设备信息泄漏漏洞案例【二】
    轻松高效搭建可视化数据网站
    【Python】操作符梅花号*、乘号*的用法汇总与小结
    深度学习(PyTorch)——Dataset&DataLoader 加载数据集
    三菱PLC slmp(mc)协议
    机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
    LCP 01.猜数字
    一个使用 c++20 的 proxy 的高性能实现
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134523243