• 聊聊logback的DynamicThresholdFilter


    本文主要研究一下logback的DynamicThresholdFilter

    DynamicThresholdFilter

    public class DynamicThresholdFilter extends TurboFilter {
        private Map valueLevelMap = new HashMap();
        private Level defaultThreshold = Level.ERROR;
        private String key;
    
        private FilterReply onHigherOrEqual = FilterReply.NEUTRAL;
        private FilterReply onLower = FilterReply.DENY;
    
        //......
    }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    DynamicThresholdFilter继承了TurboFilter,它定义了valueLevelMap、defaultThreshold为ERROR,onHigherOrEqual为NEUTRAL,onLower为DENY

    addMDCValueLevelPair

        public void addMDCValueLevelPair(MDCValueLevelPair mdcValueLevelPair) {
            if (valueLevelMap.containsKey(mdcValueLevelPair.getValue())) {
                addError(mdcValueLevelPair.getValue() + " has been already set");
            } else {
                valueLevelMap.put(mdcValueLevelPair.getValue(), mdcValueLevelPair.getLevel());
            }
        }    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    addMDCValueLevelPair方法可以根据MDCValueLevelPair往valueLevelMap添加配置

    MDCValueLevelPair

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

    public class MDCValueLevelPair {
        private String value;
        private Level level;
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String name) {
            this.value = name;
        }
    
        public Level getLevel() {
            return level;
        }
    
        public void setLevel(Level level) {
            this.level = level;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    MDCValueLevelPair定义了value及level属性

    decide

        public FilterReply decide(Marker marker, Logger logger, Level level, String s, Object[] objects,
                Throwable throwable) {
    
            String mdcValue = MDC.get(this.key);
            if (!isStarted()) {
                return FilterReply.NEUTRAL;
            }
    
            Level levelAssociatedWithMDCValue = null;
            if (mdcValue != null) {
                levelAssociatedWithMDCValue = valueLevelMap.get(mdcValue);
            }
            if (levelAssociatedWithMDCValue == null) {
                levelAssociatedWithMDCValue = defaultThreshold;
            }
            if (level.isGreaterOrEqual(levelAssociatedWithMDCValue)) {
                return onHigherOrEqual;
            } else {
                return onLower;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    decide方法先从mdc获取key对应的mdcValue,然后根据mdcValue去valueLevelMap获取对应的level,如果获取不到则取defaultThreshold;如果要打印的level大于等于levelAssociatedWithMDCValue则返回NEUTRAL,否则返回DENY

    示例

    
    
    
    
    
    	
    		userId
    		ERROR
    		
    			user1
    			INFO
    		
    		
    			user2
    			TRACE
    		
    
    	
    
    
    	
    	
    
    	
    		
    		
    	
    
    
    • 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

    小结

    logback提供了DynamicThresholdFilter,它可以根据配置的key从MDC取值,再根据配置的MDCValueLevelPair去映射对应的level,达到动态log级别的效果。

  • 相关阅读:
    目标检测YOLO实战应用案例100讲-基于YOLO的道路交通标志识别(续)
    Rome反序列化链分析
    13-Dubbo服务调用过程源码分析-服务消费方发送请求
    二十六、MySQL并发事务问题:脏读/不可重复读/幻读
    2023年【安全生产监管人员】考试题及安全生产监管人员找解析
    Java.lang.Class类 getTypeParameters()方法有什么功能呢?
    Ncnn框架在c++的推理及其认识
    Liunx中系统安全及文件系统(极其粗糙版)
    Apache Pulsar 在腾讯云上的最佳实践
    MyBatis基础之SqlSession
  • 原文地址:https://blog.csdn.net/hello_ejb3/article/details/134390894