1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config;
18
19 import java.net.URI;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.Level;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.apache.logging.log4j.core.LoggerContext;
26 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
27 import org.apache.logging.log4j.core.util.NetUtils;
28 import org.apache.logging.log4j.spi.LoggerContextFactory;
29 import org.apache.logging.log4j.status.StatusLogger;
30 import org.apache.logging.log4j.util.Strings;
31
32
33
34
35
36 public final class Configurator {
37
38 private static final String FQCN = Configurator.class.getName();
39
40 private static final Logger LOGGER = StatusLogger.getLogger();
41
42 private static Log4jContextFactory getFactory() {
43 final LoggerContextFactory factory = LogManager.getFactory();
44 if (factory instanceof Log4jContextFactory) {
45 return (Log4jContextFactory) factory;
46 } else if (factory != null) {
47 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.",
48 factory.getClass().getName(), Log4jContextFactory.class.getName());
49 return null;
50 } else {
51 LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
52 return null;
53 }
54 }
55
56
57
58
59
60
61
62 public static LoggerContext initialize(final ClassLoader loader,
63 final ConfigurationSource source) {
64 return initialize(loader, source, null);
65 }
66
67
68
69
70
71
72
73
74
75 public static LoggerContext initialize(final ClassLoader loader,
76 final ConfigurationSource source,
77 final Object externalContext)
78 {
79
80 try {
81 final Log4jContextFactory factory = getFactory();
82 return factory == null ? null :
83 factory.getContext(FQCN, loader, externalContext, false, source);
84 } catch (final Exception ex) {
85 LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, ex);
86 }
87 return null;
88 }
89
90
91
92
93
94
95
96
97 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) {
98 return initialize(name, loader, configLocation, null);
99
100 }
101
102
103
104
105
106
107
108
109
110 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation,
111 final Object externalContext) {
112 final URI uri = Strings.isBlank(configLocation) ? null : NetUtils.toURI(configLocation);
113 return initialize(name, loader, uri, externalContext);
114 }
115
116
117
118
119
120
121
122
123 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) {
124 return initialize(name, loader, configLocation, null);
125 }
126
127
128
129
130
131
132
133
134
135 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
136 final Object externalContext) {
137
138 try {
139 final Log4jContextFactory factory = getFactory();
140 return factory == null ? null :
141 factory.getContext(FQCN, loader, externalContext, false, configLocation, name);
142 } catch (final Exception ex) {
143 LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].",
144 name, configLocation, ex);
145 }
146 return null;
147 }
148
149
150
151
152
153
154
155 public static LoggerContext initialize(final String name, final String configLocation) {
156 return initialize(name, null, configLocation);
157 }
158
159
160
161
162
163
164 public static LoggerContext initialize(Configuration configuration) {
165 return initialize(null, configuration, null);
166 }
167
168
169
170
171
172
173
174 public static LoggerContext initialize(final ClassLoader loader, Configuration configuration) {
175 return initialize(loader, configuration, null);
176 }
177
178
179
180
181
182
183
184
185 public static LoggerContext initialize(final ClassLoader loader, Configuration configuration, final Object externalContext) {
186 try {
187 final Log4jContextFactory factory = getFactory();
188 return factory == null ? null :
189 factory.getContext(FQCN, loader, externalContext, false, configuration);
190 } catch (final Exception ex) {
191 LOGGER.error("There was a problem initializing the LoggerContext using configuration {}",
192 configuration.getName(), ex);
193 }
194 return null;
195 }
196
197
198
199
200
201
202 public static void setAllLevels(final String parentLogger, final Level level) {
203
204
205
206
207
208 final LoggerContext loggerContext = LoggerContext.getContext(false);
209 final Configuration config = loggerContext.getConfiguration();
210 boolean set = setLevel(parentLogger, level, config);
211 for (final Map.Entry<String, LoggerConfig> entry : config.getLoggers().entrySet()) {
212 if (entry.getKey().startsWith(parentLogger)) {
213 set |= setLevel(entry.getValue(), level);
214 }
215 }
216 if (set) {
217 loggerContext.updateLoggers();
218 }
219 }
220
221 private static boolean setLevel(final LoggerConfig loggerConfig, final Level level) {
222 final boolean set = !loggerConfig.getLevel().equals(level);
223 if (set) {
224 loggerConfig.setLevel(level);
225 }
226 return set;
227 }
228
229
230
231
232
233
234
235
236 public static void setLevel(final Map<String, Level> levelMap) {
237 final LoggerContext loggerContext = LoggerContext.getContext(false);
238 final Configuration config = loggerContext.getConfiguration();
239 boolean set = false;
240 for (final Map.Entry<String, Level> entry : levelMap.entrySet()) {
241 final String loggerName = entry.getKey();
242 final Level level = entry.getValue();
243 set |= setLevel(loggerName, level, config);
244 }
245 if (set) {
246 loggerContext.updateLoggers();
247 }
248 }
249
250
251
252
253
254
255
256
257
258 public static void setLevel(final String loggerName, final Level level) {
259 final LoggerContext loggerContext = LoggerContext.getContext(false);
260 if (Strings.isEmpty(loggerName)) {
261 setRootLevel(level);
262 } else {
263 if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
264 loggerContext.updateLoggers();
265 }
266 }
267 }
268
269 private static boolean setLevel(final String loggerName, final Level level, final Configuration config) {
270 boolean set;
271 LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
272 if (!loggerName.equals(loggerConfig.getName())) {
273
274 loggerConfig = new LoggerConfig(loggerName, level, true);
275 config.addLogger(loggerName, loggerConfig);
276 loggerConfig.setLevel(level);
277 set = true;
278 } else {
279 set = setLevel(loggerConfig, level);
280 }
281 return set;
282 }
283
284
285
286
287
288
289
290 public static void setRootLevel(final Level level) {
291 final LoggerContext loggerContext = LoggerContext.getContext(false);
292 final LoggerConfig loggerConfig = loggerContext.getConfiguration().getRootLogger();
293 if (!loggerConfig.getLevel().equals(level)) {
294 loggerConfig.setLevel(level);
295 loggerContext.updateLoggers();
296 }
297 }
298
299
300
301
302
303 public static void shutdown(final LoggerContext ctx) {
304 if (ctx != null) {
305 ctx.stop();
306 }
307 }
308
309 private Configurator() {
310
311 }
312 }