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。
以上就是logback的isDebugEnabled日志配置级别源码解析的详细内容,更多关于logback isDebugEnabled的资料请关注脚本之家其它相关文章!
相关文章
在VSCode里使用Jupyter Notebook调试Java代码的详细过程
Jupyter Notebook是以网页的形式打开,可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示的程序,这篇文章主要介绍了在VSCode里使用Jupyter Notebook,调试Java代码,需要的朋友可以参考下2022-07-07@Valid 校验无效,BindingResult未获得错误的解决
这篇文章主要介绍了@Valid 校验无效,BindingResult未获得错误的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10Spring + mybatis + mysql使用事物的几种方法总结
这篇文章主要给大家总结介绍了关于Spring + mybatis + mysql使用事物的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-05-05
最新评论