001package ch.qos.logback.classic.model.processor; 002 003import static ch.qos.logback.core.joran.JoranConstants.NULL; 004 005import ch.qos.logback.classic.Level; 006import ch.qos.logback.classic.Logger; 007import ch.qos.logback.classic.LoggerContext; 008import ch.qos.logback.classic.model.LoggerModel; 009import ch.qos.logback.core.Context; 010import ch.qos.logback.core.joran.JoranConstants; 011import ch.qos.logback.core.model.Model; 012import ch.qos.logback.core.model.processor.ModelHandlerBase; 013import ch.qos.logback.core.model.processor.ModelHandlerException; 014import ch.qos.logback.core.model.processor.ModelInterpretationContext; 015import ch.qos.logback.core.spi.ErrorCodes; 016import ch.qos.logback.core.util.OptionHelper; 017 018public class LoggerModelHandler extends ModelHandlerBase { 019 020 Logger logger; 021 boolean inError = false; 022 023 public LoggerModelHandler(Context context) { 024 super(context); 025 } 026 027 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) { 028 return new LoggerModelHandler(context); 029 } 030 031 protected Class<LoggerModel> getSupportedModelClass() { 032 return LoggerModel.class; 033 } 034 035 @Override 036 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 037 inError = false; 038 039 LoggerModel loggerModel = (LoggerModel) model; 040 041 String finalLoggerName = mic.subst(loggerModel.getName()); 042 043 LoggerContext loggerContext = (LoggerContext) this.context; 044 045 logger = loggerContext.getLogger(finalLoggerName); 046 047 String levelStr = mic.subst(loggerModel.getLevel()); 048 if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelStr)) { 049 if (JoranConstants.INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) { 050 if(Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(finalLoggerName)) { 051 addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL); 052 } else { 053 addInfo("Setting level of logger [" + finalLoggerName + "] to null, i.e. INHERITED"); 054 logger.setLevel(null); 055 } 056 } else { 057 Level level = Level.toLevel(levelStr); 058 addInfo("Setting level of logger [" + finalLoggerName + "] to " + level); 059 logger.setLevel(level); 060 } 061 } 062 063 String additivityStr = mic.subst(loggerModel.getAdditivity()); 064 if (!OptionHelper.isNullOrEmptyOrAllSpaces(additivityStr)) { 065 boolean additive = OptionHelper.toBoolean(additivityStr, true); 066 addInfo("Setting additivity of logger [" + finalLoggerName + "] to " + additive); 067 logger.setAdditive(additive); 068 } 069 070 mic.pushObject(logger); 071 } 072 073 @Override 074 public void postHandle(ModelInterpretationContext mic, Model model) { 075 if (inError) { 076 return; 077 } 078 Object o = mic.peekObject(); 079 if (o != logger) { 080 LoggerModel loggerModel = (LoggerModel) model; 081 addWarn("The object [" + o + "] on the top the of the stack is not the expected logger named " 082 + loggerModel.getName()); 083 } else { 084 mic.popObject(); 085 } 086 087 } 088 089}