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,否則執(zhí)行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的時候執(zhí)行decide方法,否則遍歷TurboFilter,挨個執(zhí)行decide,一但有DENY或者ACCEPT直接返回,否則最后返回NEUTRAL
小結
logback的isDebugEnabled先通過callTurboFilters獲取debug級別的FilterReply,若為DENY返回false,若為ACCEPT返回true,若為NEUTRAL則判斷effectiveLevelInt是否小于等于DEBUG_INT。callTurboFilters是一系列isYYYEnabled()共用的,它在turboFilterList是否為空,為空則返回NEUTRAL,在只有1個TurboFilter的時候執(zhí)行decide方法,否則遍歷TurboFilter,挨個執(zhí)行decide,一但有DENY或者ACCEPT直接返回,否則最后返回NEUTRAL。
以上就是logback的isDebugEnabled日志配置級別源碼解析的詳細內(nèi)容,更多關于logback isDebugEnabled的資料請關注腳本之家其它相關文章!
相關文章
基于Java多線程notify與notifyall的區(qū)別分析
本篇文章對Java中多線程notify與notifyall的區(qū)別進行了詳細的分析介紹。需要的朋友參考下2013-05-05
struts2中通過json傳值解決亂碼問題的實現(xiàn)方法
這篇文章主要介紹了struts2中通過json傳值解決亂碼問題的實現(xiàn)方法,涉及js編碼及java解碼的相關操作技巧,需要的朋友可以參考下2016-06-06
使用java.nio.file?庫優(yōu)雅的操作文件詳解
這篇文章主要介紹了使用java.nio.file?庫優(yōu)雅的操作文件詳解,需要的朋友可以參考下2023-05-05

