聊聊logback的isDebugEnabled

本文主要研究一下logback的isDebugEnabled

isDebugEnabled

public final class Logger
        implements org.slf4j.Logger, LocationAwareLogger, LoggingEventAware, AppenderAttachable<ILoggingEvent>, Serializable {

    //......
    
    public boolean isDebugEnabled() {
        return isDebugEnabled(null);
    }

    public boolean isDebugEnabled(Marker marker) {
        final FilterReply decision = callTurboFilters(marker, Level.DEBUG);
        if (decision == FilterReply.NEUTRAL) {
            return effectiveLevelInt <= Level.DEBUG_INT;
        } else if (decision == FilterReply.DENY) {
            return false;
        } else if (decision == FilterReply.ACCEPT) {
            return true;
        } else {
            throw new IllegalStateException("Unknown FilterReply value: " + decision);
        }
    }        

}        

isDebugEnabled先通过callTurboFilters获取debug级别的FilterReply,若为DENY返回false,若为ACCEPT返回true,若为NEUTRAL则判断effectiveLevelInt是否小于等于DEBUG_INT

callTurboFilters

    /**
     * Method that calls the attached TurboFilter objects based on the logger and
     * the level.
     * 
     * It is used by isYYYEnabled() methods.
     * 
     * It returns the typical FilterReply values: ACCEPT, NEUTRAL or DENY.
     * 
     * @param level
     * @return the reply given by the TurboFilters
     */
    private FilterReply callTurboFilters(Marker marker, Level level) {
        return loggerContext.getTurboFilterChainDecision_0_3OrMore(marker, this, level, null, null, null);
    }

callTurboFilters从loggerContext获取getTurboFilterChainDecision_0_3OrMore

getTurboFilterChainDecision_0_3OrMore

ch/qos/logback/classic/LoggerContext.java

    final FilterReply getTurboFilterChainDecision_0_3OrMore(final Marker marker, final Logger logger, final Level level,
            final String format, final Object[] params, final Throwable t) {
        if (turboFilterList.size() == 0) {
            return FilterReply.NEUTRAL;
        }
        return turboFilterList.getTurboFilterChainDecision(marker, logger, level, format, params, t);
    }

该方法先判断turboFilterList是否为空,为空则返回NEUTRAL,否则执行turboFilterList.getTurboFilterChainDecision

getTurboFilterChainDecision

ch/qos/logback/classic/spi/TurboFilterList.java

    public FilterReply getTurboFilterChainDecision(final Marker marker, final Logger logger, final Level level,
            final String format, final Object[] params, final Throwable t) {

        final int size = size();
        // if (size == 0) {
        // return FilterReply.NEUTRAL;
        // }
        if (size == 1) {
            try {
                TurboFilter tf = get(0);
                return tf.decide(marker, logger, level, format, params, t);
            } catch (IndexOutOfBoundsException iobe) {
                return FilterReply.NEUTRAL;
            }
        }

        Object[] tfa = toArray();
        final int len = tfa.length;
        for (int i = 0; i < len; i++) {
            // for (TurboFilter tf : this) {
            final TurboFilter tf = (TurboFilter) tfa[i];
            final FilterReply r = tf.decide(marker, logger, level, format, params, t);
            if (r == FilterReply.DENY || r == FilterReply.ACCEPT) {
                return r;
            }
        }
        return FilterReply.NEUTRAL;
    }

getTurboFilterChainDecision在只有1个TurboFilter的时候执行decide方法,否则遍历TurboFilter,挨个执行decide,一但有DENY或者ACCEPT直接返回,否则最后返回NEUTRAL

小结

logback的isDebugEnabled先通过callTurboFilters获取debug级别的FilterReply,若为DENY返回false,若为ACCEPT返回true,若为NEUTRAL则判断effectiveLevelInt是否小于等于DEBUG_INT。callTurboFilters是一系列isYYYEnabled()共用的,它在turboFilterList是否为空,为空则返回NEUTRAL,在只有1个TurboFilter的时候执行decide方法,否则遍历TurboFilter,挨个执行decide,一但有DENY或者ACCEPT直接返回,否则最后返回NEUTRAL。

相关推荐
激流丶12 分钟前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
Themberfue16 分钟前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
让学习成为一种生活方式33 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
晨曦_子画38 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
南宫生1 小时前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
Heavydrink1 小时前
HTTP动词与状态码
java
ktkiko111 小时前
Java中的远程方法调用——RPC详解
java·开发语言·rpc
计算机-秋大田2 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
神里大人2 小时前
idea、pycharm等软件的文件名红色怎么变绿色
java·pycharm·intellij-idea
小冉在学习2 小时前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论