1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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 v1.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  package ch.qos.logback.access.common.model.processor;
15  
16  import ch.qos.logback.access.common.model.ConfigurationModel;
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.model.Model;
19  import ch.qos.logback.core.model.processor.ModelHandlerBase;
20  import ch.qos.logback.core.model.processor.ModelHandlerException;
21  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
22  import ch.qos.logback.core.status.OnConsoleStatusListener;
23  import ch.qos.logback.core.util.OptionHelper;
24  import ch.qos.logback.core.util.StatusListenerConfigHelper;
25  
26  public class ConfigurationModelHandler extends ModelHandlerBase {
27      static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback-access.debug";
28  
29      public ConfigurationModelHandler(Context context) {
30          super(context);
31      }
32  
33      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
34          return new ConfigurationModelHandler(context);
35      }
36  
37      protected Class<ConfigurationModel> getSupportedModelClass() {
38          return ConfigurationModel.class;
39      }
40  
41      @Override
42      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
43          ConfigurationModel configurationModel = (ConfigurationModel) model;
44          // See LBCLASSIC-225 (the system property is looked up first). Thus, it overrides
45          // the equivalent property in the config file. This reversal of scope priority
46          // is justified
47          // by the use case: the admin trying to chase rogue config file
48          String debug = System.getProperty(DEBUG_SYSTEM_PROPERTY_KEY);
49          if (debug == null) {
50              debug = configurationModel.getDebug();
51          }
52          if (OptionHelper.isNullOrEmpty(debug) || debug.equals("false") || debug.equals("null")) {
53              addInfo(ConfigurationModel.INTERNAL_DEBUG_ATTR + " attribute not set");
54          } else {
55              StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
56          }
57  
58      }
59  }