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.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.apache.logging.log4j.core.LoggerContext;
29 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
30 import org.apache.logging.log4j.core.util.NetUtils;
31 import org.apache.logging.log4j.spi.LoggerContextFactory;
32 import org.apache.logging.log4j.status.StatusLogger;
33 import org.apache.logging.log4j.util.Strings;
34
35
36
37
38
39 public final class Configurator {
40
41 private static final String FQCN = Configurator.class.getName();
42
43 private static final Logger LOGGER = StatusLogger.getLogger();
44
45 private static Log4jContextFactory getFactory() {
46 final LoggerContextFactory factory = LogManager.getFactory();
47 if (factory instanceof Log4jContextFactory) {
48 return (Log4jContextFactory) factory;
49 } else if (factory != null) {
50 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.",
51 factory.getClass().getName(), Log4jContextFactory.class.getName());
52 return null;
53 } else {
54 LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
55 return null;
56 }
57 }
58
59
60
61
62
63
64
65 public static LoggerContext initialize(final ClassLoader loader,
66 final ConfigurationSource source) {
67 return initialize(loader, source, null);
68 }
69
70
71
72
73
74
75
76
77
78 public static LoggerContext initialize(final ClassLoader loader,
79 final ConfigurationSource source,
80 final Object externalContext)
81 {
82
83 try {
84 final Log4jContextFactory factory = getFactory();
85 return factory == null ? null :
86 factory.getContext(FQCN, loader, externalContext, false, source);
87 } catch (final Exception ex) {
88 LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, ex);
89 }
90 return null;
91 }
92
93
94
95
96
97
98
99
100 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) {
101 return initialize(name, loader, configLocation, null);
102
103 }
104
105
106
107
108
109
110
111
112
113 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation,
114 final Object externalContext) {
115 if (Strings.isBlank(configLocation)) {
116 return initialize(name, loader, (URI) null, externalContext);
117 }
118 if (configLocation.contains(",")) {
119 final String[] parts = configLocation.split(",");
120 String scheme = null;
121 final List<URI> uris = new ArrayList<>(parts.length);
122 for (final String part : parts) {
123 final URI uri = NetUtils.toURI(scheme != null ? scheme + ":" + part.trim() : part.trim());
124 if (scheme == null && uri.getScheme() != null) {
125 scheme = uri.getScheme();
126 }
127 uris.add(uri);
128 }
129 return initialize(name, loader, uris, externalContext);
130 }
131 return initialize(name, loader, NetUtils.toURI(configLocation), externalContext);
132 }
133
134
135
136
137
138
139
140
141 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) {
142 return initialize(name, loader, configLocation, null);
143 }
144
145
146
147
148
149
150
151
152
153 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
154 final Object externalContext) {
155
156 try {
157 final Log4jContextFactory factory = getFactory();
158 return factory == null ? null :
159 factory.getContext(FQCN, loader, externalContext, false, configLocation, name);
160 } catch (final Exception ex) {
161 LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].",
162 name, configLocation, ex);
163 }
164 return null;
165 }
166
167 public static LoggerContext initialize(final String name, final ClassLoader loader, final List<URI> configLocations,
168 final Object externalContext) {
169 try {
170 final Log4jContextFactory factory = getFactory();
171 return factory == null ?
172 null :
173 factory.getContext(FQCN, loader, externalContext, false, configLocations, name);
174 } catch (final Exception ex) {
175 LOGGER.error("There was a problem initializing the LoggerContext [{}] using configurations at [{}].", name,
176 configLocations, ex);
177 }
178 return null;
179 }
180
181
182
183
184
185
186
187 public static LoggerContext initialize(final String name, final String configLocation) {
188 return initialize(name, null, configLocation);
189 }
190
191
192
193
194
195
196 public static LoggerContext initialize(final Configuration configuration) {
197 return initialize(null, configuration, null);
198 }
199
200
201
202
203
204
205
206 public static LoggerContext initialize(final ClassLoader loader, final Configuration configuration) {
207 return initialize(loader, configuration, null);
208 }
209
210
211
212
213
214
215
216
217 public static LoggerContext initialize(final ClassLoader loader, final Configuration configuration, final Object externalContext) {
218 try {
219 final Log4jContextFactory factory = getFactory();
220 return factory == null ? null :
221 factory.getContext(FQCN, loader, externalContext, false, configuration);
222 } catch (final Exception ex) {
223 LOGGER.error("There was a problem initializing the LoggerContext using configuration {}",
224 configuration.getName(), ex);
225 }
226 return null;
227 }
228
229
230
231
232
233 public static void reconfigure() {
234 try {
235 Log4jContextFactory factory = getFactory();
236 if (factory != null) {
237 factory.getSelector().getContext(FQCN, null, false).reconfigure();
238 } else {
239 LOGGER.warn("Unable to reconfigure - Log4j has not been initialized.");
240 }
241 } catch (final Exception ex) {
242 LOGGER.error("Error encountered trying to reconfigure logging", ex);
243 }
244 }
245
246
247
248
249
250
251 public static void reconfigure(final URI uri) {
252 try {
253 Log4jContextFactory factory = getFactory();
254 if (factory != null) {
255 factory.getSelector().getContext(FQCN, null, false).setConfigLocation(uri);
256 } else {
257 LOGGER.warn("Unable to reconfigure - Log4j has not been initialized.");
258 }
259 } catch (final Exception ex) {
260 LOGGER.error("Error encountered trying to reconfigure logging", ex);
261 }
262 }
263
264
265
266
267
268
269 public static void setAllLevels(final String parentLogger, final Level level) {
270
271
272
273
274
275 final LoggerContext loggerContext = LoggerContext.getContext(false);
276 final Configuration config = loggerContext.getConfiguration();
277 boolean set = setLevel(parentLogger, level, config);
278 for (final Map.Entry<String, LoggerConfig> entry : config.getLoggers().entrySet()) {
279 if (entry.getKey().startsWith(parentLogger)) {
280 set |= setLevel(entry.getValue(), level);
281 }
282 }
283 if (set) {
284 loggerContext.updateLoggers();
285 }
286 }
287
288 private static boolean setLevel(final LoggerConfig loggerConfig, final Level level) {
289 final boolean set = !loggerConfig.getLevel().equals(level);
290 if (set) {
291 loggerConfig.setLevel(level);
292 }
293 return set;
294 }
295
296
297
298
299
300
301
302
303 public static void setLevel(final Map<String, Level> levelMap) {
304 final LoggerContext loggerContext = LoggerContext.getContext(false);
305 final Configuration config = loggerContext.getConfiguration();
306 boolean set = false;
307 for (final Map.Entry<String, Level> entry : levelMap.entrySet()) {
308 final String loggerName = entry.getKey();
309 final Level level = entry.getValue();
310 set |= setLevel(loggerName, level, config);
311 }
312 if (set) {
313 loggerContext.updateLoggers();
314 }
315 }
316
317
318
319
320
321
322
323
324
325 public static void setLevel(final String loggerName, final Level level) {
326 final LoggerContext loggerContext = LoggerContext.getContext(false);
327 if (Strings.isEmpty(loggerName)) {
328 setRootLevel(level);
329 } else {
330 if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
331 loggerContext.updateLoggers();
332 }
333 }
334 }
335
336 private static boolean setLevel(final String loggerName, final Level level, final Configuration config) {
337 boolean set;
338 LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
339 if (!loggerName.equals(loggerConfig.getName())) {
340
341 loggerConfig = new LoggerConfig(loggerName, level, true);
342 config.addLogger(loggerName, loggerConfig);
343 loggerConfig.setLevel(level);
344 set = true;
345 } else {
346 set = setLevel(loggerConfig, level);
347 }
348 return set;
349 }
350
351
352
353
354
355
356
357 public static void setRootLevel(final Level level) {
358 final LoggerContext loggerContext = LoggerContext.getContext(false);
359 final LoggerConfig loggerConfig = loggerContext.getConfiguration().getRootLogger();
360 if (!loggerConfig.getLevel().equals(level)) {
361 loggerConfig.setLevel(level);
362 loggerContext.updateLoggers();
363 }
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377 public static void shutdown(final LoggerContext ctx) {
378 if (ctx != null) {
379 ctx.stop();
380 }
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403 public static boolean shutdown(final LoggerContext ctx, final long timeout, final TimeUnit timeUnit) {
404 if (ctx != null) {
405 return ctx.stop(timeout, timeUnit);
406 }
407 return true;
408 }
409
410 private Configurator() {
411
412 }
413 }