001package ch.qos.logback.classic.model.processor;
002
003import ch.qos.logback.classic.Level;
004import ch.qos.logback.classic.Logger;
005import ch.qos.logback.classic.LoggerContext;
006import ch.qos.logback.classic.model.RootLoggerModel;
007import ch.qos.logback.core.Context;
008import ch.qos.logback.core.model.Model;
009import ch.qos.logback.core.model.processor.ModelHandlerBase;
010import ch.qos.logback.core.model.processor.ModelHandlerException;
011import ch.qos.logback.core.model.processor.ModelInterpretationContext;
012import ch.qos.logback.core.util.OptionHelper;
013
014public class RootLoggerModelHandler extends ModelHandlerBase {
015
016    Logger root;
017    boolean inError = false;
018
019    public RootLoggerModelHandler(Context context) {
020        super(context);
021    }
022
023    static public RootLoggerModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
024        return new RootLoggerModelHandler(context);
025    }
026
027    protected Class<RootLoggerModel> getSupportedModelClass() {
028        return RootLoggerModel.class;
029    }
030
031    @Override
032    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
033        inError = false;
034
035        RootLoggerModel rootLoggerModel = (RootLoggerModel) model;
036
037        LoggerContext loggerContext = (LoggerContext) this.context;
038        root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
039
040        String levelStr = mic.subst(rootLoggerModel.getLevel());
041        if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelStr)) {
042            Level level = Level.toLevel(levelStr);
043            addInfo("Setting level of ROOT logger to " + level);
044            root.setLevel(level);
045        }
046
047        mic.pushObject(root);
048    }
049
050    @Override
051    public void postHandle(ModelInterpretationContext mic, Model model) {
052        if (inError) {
053            return;
054        }
055        Object o = mic.peekObject();
056        if (o != root) {
057            addWarn("The object [" + o + "] on the top the of the stack is not the root logger");
058        } else {
059            mic.popObject();
060        }
061    }
062
063}