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 org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.core.LoggerContext;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23
24
25
26
27 public final class Configurator {
28
29 private Configurator() {
30 }
31
32
33
34
35
36
37
38
39 public static LoggerContext initialize(String name, ClassLoader loader, String configLocation) {
40
41 try {
42 URI uri = configLocation == null ? null : new URI(configLocation);
43 return initialize(name, loader, uri);
44 } catch (URISyntaxException ex) {
45 ex.printStackTrace();
46 }
47 return null;
48 }
49
50
51
52
53
54
55
56
57 public static LoggerContext initialize(String name, ClassLoader loader, URI configLocation) {
58
59 try {
60 LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
61 Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
62 ctx.setConfiguration(config);
63 return ctx;
64 } catch (Exception ex) {
65 ex.printStackTrace();
66 }
67 return null;
68 }
69
70
71
72
73
74
75
76 public static LoggerContext initialize(ClassLoader loader, ConfigurationFactory.ConfigurationSource source) {
77
78 try {
79 LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
80 Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
81 ctx.setConfiguration(config);
82 return ctx;
83 } catch (Exception ex) {
84 ex.printStackTrace();
85 }
86 return null;
87 }
88
89
90
91
92
93 public static void shutdown(LoggerContext ctx) {
94 if (ctx != null) {
95 ctx.setConfiguration(new DefaultConfiguration());
96 }
97 }
98
99 }