前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊logback的DynamicThresholdFilter

聊聊logback的DynamicThresholdFilter

作者头像
code4it
发布2023-11-14 12:45:09
1440
发布2023-11-14 12:45:09
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下logback的DynamicThresholdFilter

DynamicThresholdFilter

代码语言:javascript
复制
public class DynamicThresholdFilter extends TurboFilter {
    private Map<String, Level> valueLevelMap = new HashMap<String, Level>();
    private Level defaultThreshold = Level.ERROR;
    private String key;

    private FilterReply onHigherOrEqual = FilterReply.NEUTRAL;
    private FilterReply onLower = FilterReply.DENY;

    //......
}    

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

addMDCValueLevelPair

代码语言:javascript
复制
    public void addMDCValueLevelPair(MDCValueLevelPair mdcValueLevelPair) {
        if (valueLevelMap.containsKey(mdcValueLevelPair.getValue())) {
            addError(mdcValueLevelPair.getValue() + " has been already set");
        } else {
            valueLevelMap.put(mdcValueLevelPair.getValue(), mdcValueLevelPair.getLevel());
        }
    }    

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

MDCValueLevelPair

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

代码语言:javascript
复制
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;
    }
}

MDCValueLevelPair定义了value及level属性

decide

代码语言:javascript
复制
    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;
        }
    }

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

示例

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>

	<turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter">
		<Key>userId</Key>
		<DefaultThreshold>ERROR</DefaultThreshold>
		<MDCValueLevelPair>
			<value>user1</value>
			<level>INFO</level>
		</MDCValueLevelPair>
		<MDCValueLevelPair>
			<value>user2</value>
			<level>TRACE</level>
		</MDCValueLevelPair>

	</turboFilter>


	<appender name="LIST"
		class="ch.qos.logback.core.read.ListAppender">
	</appender>

	<root>
		<level value="DEBUG" />
		<appender-ref ref="LIST" />
	</root>
</configuration>

小结

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-11-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • DynamicThresholdFilter
  • addMDCValueLevelPair
    • MDCValueLevelPair
    • decide
    • 示例
    • 小结
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档