logback的addtivity屬性定義源碼解讀
序
本文主要研究一下logback的addtivity屬性
LoggerModel
ch/qos/logback/classic/model/LoggerModel.java
@PhaseIndicator(phase = ProcessingPhase.SECOND)
public class LoggerModel extends Model {
private static final long serialVersionUID = 5326913660697375316L;
String name;
String level;
String additivity;
//......
}LoggerModel定義了additivity屬性
LoggerAction
ch/qos/logback/classic/joran/action/LoggerAction.java
public class LoggerAction extends BaseModelAction {
@Override
protected boolean validPreconditions(SaxEventInterpretationContext ic, String name, Attributes attributes) {
PreconditionValidator validator = new PreconditionValidator(this, ic, name, attributes);
validator.validateNameAttribute();
return validator.isValid();
}
@Override
protected Model buildCurrentModel(SaxEventInterpretationContext interpretationContext, String name,
Attributes attributes) {
LoggerModel loggerModel = new LoggerModel();
String nameStr = attributes.getValue(NAME_ATTRIBUTE);
loggerModel.setName(nameStr);
String levelStr = attributes.getValue(JoranConstants.LEVEL_ATTRIBUTE);
loggerModel.setLevel(levelStr);
String additivityStr = attributes.getValue(JoranConstants.ADDITIVITY_ATTRIBUTE);
loggerModel.setAdditivity(additivityStr);
return loggerModel;
}
}LoggerAction的buildCurrentModel方法會(huì)讀取additivity屬性,然后設(shè)置到loggerModel
LoggerModelHandler
ch/qos/logback/classic/model/processor/LoggerModelHandler.java
public class LoggerModelHandler extends ModelHandlerBase {
Logger logger;
boolean inError = false;
//......
@Override
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
inError = false;
LoggerModel loggerModel = (LoggerModel) model;
String finalLoggerName = mic.subst(loggerModel.getName());
LoggerContext loggerContext = (LoggerContext) this.context;
logger = loggerContext.getLogger(finalLoggerName);
String levelStr = mic.subst(loggerModel.getLevel());
if (!OptionHelper.isNullOrEmpty(levelStr)) {
if (JoranConstants.INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
if(Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(finalLoggerName)) {
addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL);
} else {
addInfo("Setting level of logger [" + finalLoggerName + "] to null, i.e. INHERITED");
logger.setLevel(null);
}
} else {
Level level = Level.toLevel(levelStr);
addInfo("Setting level of logger [" + finalLoggerName + "] to " + level);
logger.setLevel(level);
}
}
String additivityStr = mic.subst(loggerModel.getAdditivity());
if (!OptionHelper.isNullOrEmpty(additivityStr)) {
boolean additive = OptionHelper.toBoolean(additivityStr, true);
addInfo("Setting additivity of logger [" + finalLoggerName + "] to " + additive);
logger.setAdditive(additive);
}
mic.pushObject(logger);
}
//......
}LoggerModelHandler的handle方法會(huì)讀取additivityStr,然后設(shè)置到logger中
Logger
ch/qos/logback/classic/Logger.java
public final class Logger
implements org.slf4j.Logger, LocationAwareLogger, LoggingEventAware, AppenderAttachable<ILoggingEvent>, Serializable {
//......
/**
* The parent of this category. All categories have at least one ancestor which
* is the root category.
*/
transient private Logger parent;
/**
* Additivity is set to true by default, that is children inherit the appenders
* of their ancestors by default. If this variable is set to <code>false</code>
* then the appenders located in the ancestors of this logger will not be used.
* However, the children of this logger will inherit its appenders, unless the
* children have their additivity flag set to <code>false</code> too. See the
* user manual for more details.
*/
transient private boolean additive = true;
//......
public void callAppenders(ILoggingEvent event) {
int writes = 0;
for (Logger l = this; l != null; l = l.parent) {
writes += l.appendLoopOnAppenders(event);
if (!l.additive) {
break;
}
}
// No appenders in hierarchy
if (writes == 0) {
loggerContext.noAppenderDefinedWarning(this);
}
}
void recursiveReset() {
detachAndStopAllAppenders();
localLevelReset();
additive = true;
if (childrenList == null) {
return;
}
for (Logger childLogger : childrenList) {
childLogger.recursiveReset();
}
}
//......
}Logger的callAppenders方法會(huì)先打印自己的appender,然后逐層遍歷parent進(jìn)行打印,若additive為false則不通過(guò)parent的appender打印
小結(jié)
logback的Logger提供了addtivity屬性,默認(rèn)為true,即除了自己appender,還會(huì)通過(guò)parent的appender進(jìn)行打印,設(shè)置為false則不通過(guò)parent的appender進(jìn)行打印。
以上就是logback的addtivity屬性源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于logback addtivity屬性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot集成Redis向量數(shù)據(jù)庫(kù)實(shí)現(xiàn)相似性搜索功能
Redis?是一個(gè)開(kāi)源(BSD?許可)的內(nèi)存數(shù)據(jù)結(jié)構(gòu)存儲(chǔ),用作數(shù)據(jù)庫(kù)、緩存、消息代理和流式處理引擎,向量檢索的核心原理是通過(guò)將文本或數(shù)據(jù)表示為高維向量,并在查詢時(shí)根據(jù)向量的相似度進(jìn)行搜索,本文給大家介紹了SpringBoot集成Redis向量數(shù)據(jù)庫(kù)實(shí)現(xiàn)相似性搜索功能2024-09-09
springboot 無(wú)法掃描到父類模塊中Bean的原因及解決
這篇文章主要介紹了springboot 無(wú)法掃描到父類模塊中Bean的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Pattern.compile函數(shù)提取字符串中指定的字符(推薦)
這篇文章主要介紹了Pattern.compile函數(shù)提取字符串中指定的字符,使用的是Java中的Pattern.compile函數(shù)來(lái)實(shí)現(xiàn)對(duì)指定字符串的截取,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
java swing實(shí)現(xiàn)簡(jiǎn)單計(jì)算器界面
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)簡(jiǎn)單計(jì)算器界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java滾動(dòng)數(shù)組計(jì)算編輯距離操作示例
這篇文章主要介紹了Java滾動(dòng)數(shù)組計(jì)算編輯距離操作,涉及java字符串與數(shù)組的遍歷、計(jì)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
java8如何根據(jù)list對(duì)象中的屬性過(guò)濾篩選
這篇文章主要介紹了java8如何根據(jù)list對(duì)象中的屬性過(guò)濾篩選,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求
這篇文章主要介紹了springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
使用Jitpack發(fā)布開(kāi)源Java庫(kù)的詳細(xì)流程
這篇文章主要介紹了使用Jitpack發(fā)布開(kāi)源Java庫(kù)的詳細(xì)流程,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

