001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.classic.model.processor;
016
017import ch.qos.logback.classic.Level;
018import ch.qos.logback.classic.Logger;
019import ch.qos.logback.classic.LoggerContext;
020import ch.qos.logback.classic.model.RootLoggerModel;
021import ch.qos.logback.core.Context;
022import ch.qos.logback.core.model.Model;
023import ch.qos.logback.core.model.processor.ModelHandlerBase;
024import ch.qos.logback.core.model.processor.ModelHandlerException;
025import ch.qos.logback.core.model.processor.ModelInterpretationContext;
026import ch.qos.logback.core.util.OptionHelper;
027
028public class RootLoggerModelHandler extends ModelHandlerBase {
029
030    Logger root;
031    boolean inError = false;
032
033    public RootLoggerModelHandler(Context context) {
034        super(context);
035    }
036
037    static public RootLoggerModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
038        return new RootLoggerModelHandler(context);
039    }
040
041    protected Class<RootLoggerModel> getSupportedModelClass() {
042        return RootLoggerModel.class;
043    }
044
045    @Override
046    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
047        inError = false;
048
049        RootLoggerModel rootLoggerModel = (RootLoggerModel) model;
050
051        LoggerContext loggerContext = (LoggerContext) this.context;
052        root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
053
054        String levelStr = mic.subst(rootLoggerModel.getLevel());
055        if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelStr)) {
056            Level level = Level.toLevel(levelStr);
057            addInfo("Setting level of ROOT logger to " + level);
058            root.setLevel(level);
059        }
060
061        mic.pushObject(root);
062    }
063
064    @Override
065    public void postHandle(ModelInterpretationContext mic, Model model) {
066        if (inError) {
067            return;
068        }
069        Object o = mic.peekObject();
070        if (o != root) {
071            addWarn("The object [" + o + "] on the top the of the stack is not the root logger");
072        } else {
073            mic.popObject();
074        }
075    }
076
077}