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.joran.JoranConfigurator;
21 import ch.qos.logback.classic.model.ConfigurationModel;
22 import ch.qos.logback.classic.util.LevelUtil;
23 import ch.qos.logback.core.Context;
24 import ch.qos.logback.core.joran.GenericXMLConfigurator;
25 import ch.qos.logback.core.model.Model;
26 import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
27 import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
28 import ch.qos.logback.core.spi.ContextAwareBase;
29 import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
30 import ch.qos.logback.core.status.OnConsoleStatusListener;
31 import ch.qos.logback.core.util.OptionHelper;
32 import ch.qos.logback.core.util.StatusListenerConfigHelper;
33 import ch.qos.logback.core.util.StringUtil;
34
35 import java.util.Map;
36 import java.util.function.Supplier;
37
38 public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {
39
40 public static final String SET_CONTEXT_METHOD_NAME = "setContext";
41 public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName";
42 public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger";
43 public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper";
44 public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper";
45
46
47 protected VariableSubstitutionsHelper variableSubstitutionsHelper;
48
49 protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
50
51 protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) {
52 LoggerContext loggerContext = (LoggerContext) context;
53 Logger logger = loggerContext.getLogger(loggerName);
54 if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) {
55 Level level = LevelUtil.levelStringToLevel(levelString);
56 logger.setLevel(level);
57 }
58 if (additivity != null) {
59 logger.setAdditive(additivity);
60 }
61 return logger;
62 }
63
64 @Override
65 public void setContext(Context context) {
66 super.setContext(context);
67 variableSubstitutionsHelper = new VariableSubstitutionsHelper(context);
68 propertyModelHandlerHelper.setContext(context);
69 }
70
71 protected void setContextName(String name) {
72 if (StringUtil.isNullOrEmpty(name)) {
73 addError("Cannot set context name to null or empty string");
74 return;
75 }
76 try {
77 String substName = subst(name);
78 addInfo("Setting context name to [" + substName + "]");
79 context.setName(substName);
80 } catch (IllegalStateException e) {
81 addError("Failed to rename context as [" + name + "]");
82 }
83 }
84
85 protected void addOnConsoleStatusListener() {
86 StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
87 }
88
89
90
91
92
93
94
95 @Override
96 public String subst(String ref) {
97 return variableSubstitutionsHelper.subst(ref);
98 }
99
100 @Override
101 public void addSubstitutionProperty(String key, String value) {
102 variableSubstitutionsHelper.addSubstitutionProperty(key, value);
103 }
104
105
106
107
108 @Override
109 public String getProperty(String key) {
110 return variableSubstitutionsHelper.getProperty(key);
111 }
112
113 @Override
114 public Map<String, String> getCopyOfPropertyMap() {
115 return variableSubstitutionsHelper.getCopyOfPropertyMap();
116 }
117
118 public boolean isNull(String k) {
119 String val = OptionHelper.propertyLookup(k, this, context);
120 return (val == null);
121 }
122
123
124
125
126
127
128
129
130 public boolean isDefined(String k) {
131 String val = OptionHelper.propertyLookup(k, this, context);
132 return (val != null);
133 }
134
135
136
137
138
139
140
141
142 public String p(String k) {
143 return property(k);
144 }
145
146
147
148
149
150
151
152
153
154 public String property(String k) {
155 String val = OptionHelper.propertyLookup(k, this, context);
156 if (val != null)
157 return val;
158 else
159 return "";
160 }
161
162 private JoranConfigurator makeAnotherInstance() {
163 JoranConfigurator jc = new JoranConfigurator();
164 jc.setContext(context);
165 return jc;
166 }
167
168
169
170
171
172
173 @Override
174 public Supplier<? extends GenericXMLConfigurator> getConfiguratorSupplier() {
175 Supplier<? extends GenericXMLConfigurator> supplier = () -> this.makeAnotherInstance();
176 return supplier;
177 }
178
179 protected void processModelFromIncludedFile(Model modelFromIncludedFile) {
180 Supplier<? extends GenericXMLConfigurator > configuratorSupplier = this.getConfiguratorSupplier();
181 GenericXMLConfigurator genericXMLConfigurator = configuratorSupplier.get();
182 ConfigurationModel configururationModel = new ConfigurationModel();
183 configururationModel.addSubModel(modelFromIncludedFile);
184 genericXMLConfigurator.processModel(configururationModel);
185 }
186 }