View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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  
15  package ch.qos.logback.classic.tyler;
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.util.LevelUtil;
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
23  import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
24  import ch.qos.logback.core.spi.ContextAwareBase;
25  import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
26  import ch.qos.logback.core.status.OnConsoleStatusListener;
27  import ch.qos.logback.core.util.OptionHelper;
28  import ch.qos.logback.core.util.StatusListenerConfigHelper;
29  import ch.qos.logback.core.util.StringUtil;
30  
31  import java.util.Map;
32  
33  public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {
34  
35      public static final String SET_CONTEXT_METHOD_NAME = "setContext";
36      public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName";
37      public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger";
38      public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper";
39      public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper";
40  
41      // initialized via #setContext
42      protected VariableSubstitutionsHelper variableSubstitutionsHelper;
43      // context set in #setContext
44      protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
45  
46      protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) {
47          LoggerContext loggerContext = (LoggerContext) context;
48          Logger logger = loggerContext.getLogger(loggerName);
49          if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) {
50              Level level = LevelUtil.levelStringToLevel(levelString);
51              logger.setLevel(level);
52          }
53          if (additivity != null) {
54              logger.setAdditive(additivity);
55          }
56          return logger;
57      }
58  
59      @Override
60      public void setContext(Context context) {
61          super.setContext(context);
62          variableSubstitutionsHelper = new VariableSubstitutionsHelper(context);
63          propertyModelHandlerHelper.setContext(context);
64      }
65  
66      protected void setContextName(String name) {
67          if(StringUtil.isNullOrEmpty(name)) {
68              addError("Cannot set context name to null or empty string");
69              return;
70          }
71          try {
72              String substName = subst(name);
73              addInfo("Setting context name to ["+substName+"]");
74              context.setName(substName);
75          } catch (IllegalStateException e) {
76              addError("Failed to rename context as [" + name + "]");
77          }
78      }
79  
80      protected void addOnConsoleStatusListener() {
81          StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
82      }
83  
84      /**
85       * Performs variable substitution.
86       *
87       * @param ref
88       * @return
89       */
90      @Override
91      public String subst(String ref) {
92          return variableSubstitutionsHelper.subst(ref);
93      }
94  
95      @Override
96      public void addSubstitutionProperty(String key, String value) {
97          variableSubstitutionsHelper.addSubstitutionProperty(key, value);
98      }
99  
100     /**
101      * If a key is found in propertiesMap then return it.
102      */
103     @Override
104     public String getProperty(String key) {
105         return variableSubstitutionsHelper.getProperty(key);
106     }
107 
108     @Override
109     public Map<String, String> getCopyOfPropertyMap() {
110         return variableSubstitutionsHelper.getCopyOfPropertyMap();
111     }
112 
113     public boolean isNull(String k) {
114         String val = OptionHelper.propertyLookup(k, this, context);
115         return (val == null);
116     }
117 
118     /**
119      * Method used in conditional evaluation
120      *
121      * @param k  a property name
122      * @return true if the property is defined
123      * @since 1.5.4
124      */
125     public boolean isDefined(String k) {
126         String val = OptionHelper.propertyLookup(k, this, context);
127         return (val != null);
128     }
129 
130     /**
131      * Shorthand for {@link #property(String)}.
132      *
133      * @param k a property name
134      * @return value of property k
135      * @since 1.5.4
136      */
137     public String p(String k) {
138         return property(k);
139     }
140 
141     /**
142      * Return the value of the property named k. If the value is null, then the
143      * empty string is returned to avoid null checks.
144      *
145      * @param k property name
146      * @return the value of the property named k
147      * @since 1.5.4
148      */
149     public String property(String k) {
150         String val = OptionHelper.propertyLookup(k, this, context);
151         if (val != null)
152             return val;
153         else
154             return "";
155     }
156 }