当前位置: 首页 > news >正文

聊聊logback的DuplicateMessageFilter

本文主要研究一下logback的DuplicateMessageFilter

TurboFilter

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

public abstract class TurboFilter extends ContextAwareBase implements LifeCycle {private String name;boolean start = false;/*** Make a decision based on the multiple parameters passed as arguments. The* returned value should be one of <code>{@link FilterReply#DENY}</code>,* <code>{@link FilterReply#NEUTRAL}</code>, or* <code>{@link FilterReply#ACCEPT}</code>.* * @param marker* @param logger* @param level* @param format* @param params* @param t* @return*/public abstract FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params,Throwable t);public void start() {this.start = true;}public boolean isStarted() {return this.start;}public void stop() {this.start = false;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

TurboFilter继承了ContextAwareBase,声明实现LifeCycle接口,它定义了decide方法由子类实现

DuplicateMessageFilter

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

public class DuplicateMessageFilter extends TurboFilter {/*** The default cache size.*/public static final int DEFAULT_CACHE_SIZE = 100;/*** The default number of allows repetitions.*/public static final int DEFAULT_ALLOWED_REPETITIONS = 5;public int allowedRepetitions = DEFAULT_ALLOWED_REPETITIONS;public int cacheSize = DEFAULT_CACHE_SIZE;private LRUMessageCache msgCache;@Overridepublic void start() {msgCache = new LRUMessageCache(cacheSize);super.start();}@Overridepublic void stop() {msgCache.clear();msgCache = null;super.stop();}@Overridepublic FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {int count = msgCache.getMessageCountAndThenIncrement(format);if (count <= allowedRepetitions) {return FilterReply.NEUTRAL;} else {return FilterReply.DENY;}}public int getAllowedRepetitions() {return allowedRepetitions;}/*** The allowed number of repetitions before* * @param allowedRepetitions*/public void setAllowedRepetitions(int allowedRepetitions) {this.allowedRepetitions = allowedRepetitions;}public int getCacheSize() {return cacheSize;}public void setCacheSize(int cacheSize) {this.cacheSize = cacheSize;}}

DuplicateMessageFilter继承了TurboFilter,它使用了LRUMessageCache(默认大小100)来缓存format,decide方法会执行getMessageCountAndThenIncrement,若超出allowedRepetitions(默认5)则返回FilterReply.DENY,否则返回FilterReply.NEUTRAL

LRUMessageCache

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

class LRUMessageCache extends LinkedHashMap<String, Integer> {private static final long serialVersionUID = 1L;final int cacheSize;LRUMessageCache(int cacheSize) {super((int) (cacheSize * (4.0f / 3)), 0.75f, true);if (cacheSize < 1) {throw new IllegalArgumentException("Cache size cannot be smaller than 1");}this.cacheSize = cacheSize;}int getMessageCountAndThenIncrement(String msg) {// don't insert null elementsif (msg == null) {return 0;}Integer i;// LinkedHashMap is not LinkedHashMap. See also LBCLASSIC-255synchronized (this) {i = super.get(msg);if (i == null) {i = 0;} else {i = i + 1;}super.put(msg, i);}return i;}// called indirectly by get() or put() which are already supposed to be// called from within a synchronized blockprotected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {return (size() > cacheSize);}@Overridesynchronized public void clear() {super.clear();}
}

LRUMessageCache继承了LinkedHashMap,其初始size为cacheSize * (4.0f / 3),getMessageCountAndThenIncrement方法内部通过synchronized加锁获取指定msg的次数,不存在则设置为0,存在则递增;其removeEldestEntry方法判断size() > cacheSize

小结

DuplicateMessageFilter继承了TurboFilter,它使用了LRUMessageCache(默认大小100)来缓存format,decide方法会执行getMessageCountAndThenIncrement,若超出allowedRepetitions(默认5)则返回FilterReply.DENY,否则返回FilterReply.NEUTRAL。

http://www.lryc.cn/news/228945.html

相关文章:

  • WordPress 文档主题模板Red Line -v0.2.2
  • 网络和Linux网络_1(网络基础)网络概念+协议概念+网络通信原理
  • AI生成PPT工具——Gamma,结合GPT生成不错的效果
  • DcatAdmin使用模版文件时模板标签不生效
  • 【算法】算法题-20231114
  • 时序数据库 TDengine + 高级分析软件 Seeq,助力企业挖掘时序数据潜力
  • 【Rust 日报】2023-11-12 socketioxide
  • Redis快速入门(基础篇)
  • (三)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • SpringBoot--中间件技术-3:整合mongodb,整合ElasticSearch,附案例含代码(简单易懂)
  • matlab 二自由度操纵稳定性汽车模型
  • 超越任务调度的极致:初探分布式定时任务 XXL-JOB 分片广播
  • 设计模式-备忘录模式(Memento)
  • 【机器学习】正则化到底是什么?
  • Rust5.2 Generic Types, Traits, and Lifetimes
  • c 实用化的摄像头生成avi视频程序(加入精确的时间控制)
  • Web后端开发_01
  • 二十、泛型(6)
  • Java18新增特性
  • springboot容器
  • Windows 10 下使用Visual Studio 2017 编译CEF SDK
  • 数字货币swap交易所逻辑系统开发分析方案
  • spring boot中使用Bean Validation做优雅的参数校验
  • 搜索引擎项目
  • 7.外部存储器,Cache,虚拟存储器
  • UITableView的style是UITableViewStyleGrouped
  • Java17新增特性
  • VR全景技术在城市园区发展中有哪些应用与帮助
  • 在 SQL 中,当复合主键成为外键时应该如何被其它表引用
  • Ps:通过显示大小了解图像的打印尺寸