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 static ch.qos.logback.core.joran.JoranConstants.INHERITED;
18  import static ch.qos.logback.core.joran.JoranConstants.NULL;
19  
20  import ch.qos.logback.classic.Level;
21  import ch.qos.logback.classic.Logger;
22  import ch.qos.logback.classic.model.LevelModel;
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.model.Model;
25  import ch.qos.logback.core.model.processor.ModelHandlerBase;
26  import ch.qos.logback.core.model.processor.ModelHandlerException;
27  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
28  import ch.qos.logback.core.spi.ErrorCodes;
29  
30  public class LevelModelHandler extends ModelHandlerBase {
31  
32      boolean inError = false;
33  
34      public LevelModelHandler(Context context) {
35          super(context);
36      }
37  
38      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
39          return new LevelModelHandler(context);
40      }
41  
42      @Override
43      protected Class<? extends LevelModel> getSupportedModelClass() {
44          return LevelModel.class;
45      }
46  
47      @Override
48      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
49  
50          Object o = mic.peekObject();
51  
52          if (!(o instanceof Logger)) {
53              inError = true;
54              addError("For element <level>, could not find a logger at the top of execution stack.");
55              return;
56          }
57  
58          Logger l = (Logger) o;
59          String loggerName = l.getName();
60  
61          LevelModel levelModel = (LevelModel) model;
62          String levelStr = mic.subst(levelModel.getValue());
63          if (INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
64              if(Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(loggerName))
65                  addError(ErrorCodes.ROOT_LEVEL_CANNOT_BE_SET_TO_NULL);
66              else
67                 l.setLevel(null);
68          } else {
69              l.setLevel(Level.toLevel(levelStr, Level.DEBUG));
70          }
71  
72          addInfo(loggerName + " level set to " + l.getLevel());
73  
74      }
75  
76  }