001package ch.qos.logback.classic.model.processor; 002 003import static ch.qos.logback.core.joran.JoranConstants.INHERITED; 004import static ch.qos.logback.core.joran.JoranConstants.NULL; 005 006import ch.qos.logback.classic.Level; 007import ch.qos.logback.classic.Logger; 008import ch.qos.logback.classic.model.LevelModel; 009import ch.qos.logback.core.Context; 010import ch.qos.logback.core.model.Model; 011import ch.qos.logback.core.model.processor.ModelHandlerBase; 012import ch.qos.logback.core.model.processor.ModelHandlerException; 013import ch.qos.logback.core.model.processor.ModelInterpretationContext; 014import ch.qos.logback.core.spi.ErrorCodes; 015 016public class LevelModelHandler extends ModelHandlerBase { 017 018 boolean inError = false; 019 020 public LevelModelHandler(Context context) { 021 super(context); 022 } 023 024 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 025 return new LevelModelHandler(context); 026 } 027 028 @Override 029 protected Class<? extends LevelModel> getSupportedModelClass() { 030 return LevelModel.class; 031 } 032 033 @Override 034 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 035 036 Object o = mic.peekObject(); 037 038 if (!(o instanceof Logger)) { 039 inError = true; 040 addError("For element <level>, could not find a logger at the top of execution stack."); 041 return; 042 } 043 044 Logger l = (Logger) o; 045 String loggerName = l.getName(); 046 047 LevelModel levelModel = (LevelModel) model; 048 String levelStr = mic.subst(levelModel.getValue()); 049 if (INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) { 050 if(Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(loggerName)) 051 addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL); 052 else 053 l.setLevel(null); 054 } else { 055 l.setLevel(Level.toLevel(levelStr, Level.DEBUG)); 056 } 057 058 addInfo(loggerName + " level set to " + l.getLevel()); 059 060 } 061 062}