logback的DuplicateMessageFilter日志过滤操作源码解读

 更新时间:2023年11月13日 09:50:15   作者:codecraft  
这篇文章主要为大家介绍了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;
    @Override
    public void start() {
        msgCache = new LRUMessageCache(cacheSize);
        super.start();
    }
    @Override
    public void stop() {
        msgCache.clear();
        msgCache = null;
        super.stop();
    }
    @Override
    public 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 elements
        if (msg == null) {
            return 0;
        }
        Integer i;
        // LinkedHashMap is not LinkedHashMap. See also LBCLASSIC-255
        synchronized (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 block
    protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
        return (size() > cacheSize);
    }
    @Override
    synchronized 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。

以上就是logback的DuplicateMessageFilter的详细内容,更多关于logback的DuplicateMessageFilter的资料请关注脚本之家其它相关文章!

相关文章

  • 开放封闭原则_动力节点Java学院整理

    开放封闭原则_动力节点Java学院整理

    这篇文章主要介绍了开放封闭原则,开放-封闭原则是面向对象设计的核心所在,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • java.net.UnknownHostException异常的一般原因及解决步骤

    java.net.UnknownHostException异常的一般原因及解决步骤

    关于java.net.UnknownHostException大家也许都比较熟悉,这篇文章主要给大家介绍了关于java.net.UnknownHostException异常的一般原因及解决步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • Spring Boot整合持久层之JdbcTemplate多数据源

    Spring Boot整合持久层之JdbcTemplate多数据源

    持久层是JavaEE中访问数据库的核心操作,SpringBoot中对常见的持久层框架都提供了自动化配置,例如JdbcTemplate、JPA 等,MyBatis 的自动化配置则是MyBatis官方提供的。接下来分别向读者介绍Spring Boot整合这持久层技术中的整合JdbcTemplate
    2022-08-08
  • 一篇文章带你了解Spring AOP 的注解

    一篇文章带你了解Spring AOP 的注解

    这篇文章主要为大家介绍了vue组件通信的几种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • java简单工厂模式实例及讲解

    java简单工厂模式实例及讲解

    这篇文章主要为大家详细介绍了java简单工厂模式实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • IDEA标签tabs多行显示的设置

    IDEA标签tabs多行显示的设置

    这篇文章主要介绍了IDEA标签tabs多行显示的设置方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Java设计模式之Builder建造者模式

    Java设计模式之Builder建造者模式

    这篇文章主要为大家详细介绍了Java设计模式之Builder建造者模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • 一次 Java 服务性能优化实例详解

    一次 Java 服务性能优化实例详解

    这篇文章主要介绍了一次 Java 服务性能优化实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • SpringBoot中使用JWT的实战

    SpringBoot中使用JWT的实战

    本文主要介绍了SpringBoot中使用JWT的实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java使用雪花id生成算法详解

    Java使用雪花id生成算法详解

    SnowFlake算法,是Twitter开源的分布式id生成算法,在2014年开源,开源的版本由scala编写。其核心思想就是-使用一个64bit的long型的数字作为全局唯一id
    2022-12-12

最新评论