1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.model.processor;
16  
17  import ch.qos.logback.classic.Level;
18  import ch.qos.logback.classic.Logger;
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.model.RootLoggerModel;
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.model.Model;
23  import ch.qos.logback.core.model.processor.ModelHandlerBase;
24  import ch.qos.logback.core.model.processor.ModelHandlerException;
25  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
26  import ch.qos.logback.core.util.OptionHelper;
27  
28  public class RootLoggerModelHandler extends ModelHandlerBase {
29  
30      Logger root;
31      boolean inError = false;
32  
33      public RootLoggerModelHandler(Context context) {
34          super(context);
35      }
36  
37      static public RootLoggerModelHandler makeInstance(Context context, ModelInterpretationContext ic) {
38          return new RootLoggerModelHandler(context);
39      }
40  
41      protected Class<RootLoggerModel> getSupportedModelClass() {
42          return RootLoggerModel.class;
43      }
44  
45      @Override
46      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
47          inError = false;
48  
49          RootLoggerModel rootLoggerModel = (RootLoggerModel) model;
50  
51          LoggerContext loggerContext = (LoggerContext) this.context;
52          root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
53  
54          String levelStr = mic.subst(rootLoggerModel.getLevel());
55          if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelStr)) {
56              Level level = Level.toLevel(levelStr);
57              addInfo("Setting level of ROOT logger to " + level);
58              root.setLevel(level);
59          }
60  
61          mic.pushObject(root);
62      }
63  
64      @Override
65      public void postHandle(ModelInterpretationContext mic, Model model) {
66          if (inError) {
67              return;
68          }
69          Object o = mic.peekObject();
70          if (o != root) {
71              addWarn("The object [" + o + "] on the top the of the stack is not the root logger");
72          } else {
73              mic.popObject();
74          }
75      }
76  
77  }