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.model.ContextNameModel;
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.model.Model;
20  import ch.qos.logback.core.model.processor.ModelHandlerBase;
21  import ch.qos.logback.core.model.processor.ModelHandlerException;
22  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
23  
24  public class ContextNameModelHandler extends ModelHandlerBase {
25  
26      public ContextNameModelHandler(Context context) {
27          super(context);
28      }
29  
30      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
31          return new ContextNameModelHandler(context);
32      }
33  
34      @Override
35      protected Class<ContextNameModel> getSupportedModelClass() {
36          return ContextNameModel.class;
37      }
38  
39      @Override
40      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
41          ContextNameModel contextNameModel = (ContextNameModel) model;
42  
43          String finalBody = mic.subst(contextNameModel.getBodyText());
44          addInfo("Setting logger context name as [" + finalBody + "]");
45          try {
46              context.setName(finalBody);
47          } catch (IllegalStateException e) {
48              addError("Failed to rename context [" + context.getName() + "] as [" + finalBody + "]", e);
49          }
50  
51      }
52  
53  }