1
2
3
4
5
6
7
8
9
10
11
12
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
42 protected VariableSubstitutionsHelper variableSubstitutionsHelper;
43
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
86
87
88
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
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
120
121
122
123
124
125 public boolean isDefined(String k) {
126 String val = OptionHelper.propertyLookup(k, this, context);
127 return (val != null);
128 }
129
130
131
132
133
134
135
136
137 public String p(String k) {
138 return property(k);
139 }
140
141
142
143
144
145
146
147
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 }