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.net.URISyntaxException;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.core.LoggerContext;
24 import org.apache.logging.log4j.core.impl.ContextAnchor;
25 import org.apache.logging.log4j.status.StatusLogger;
26
27
28
29
30 public final class Configurator {
31
32 protected static final StatusLogger LOGGER = StatusLogger.getLogger();
33
34 private Configurator() {
35 }
36
37
38
39
40
41
42
43
44
45 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) {
46 return initialize(name, loader, configLocation, null);
47
48 }
49
50
51
52
53
54
55
56
57
58 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation,
59 final Object externalContext) {
60
61 try {
62 final URI uri = configLocation == null ? null : new URI(configLocation);
63 return initialize(name, loader, uri, externalContext);
64 } catch (final URISyntaxException ex) {
65 ex.printStackTrace();
66 }
67 return null;
68 }
69
70
71
72
73
74
75
76 public static LoggerContext initialize(final String name, final String configLocation) {
77 return initialize(name, null, configLocation);
78 }
79
80
81
82
83
84
85
86
87 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) {
88 return initialize(name, loader, configLocation, null);
89 }
90
91
92
93
94
95
96
97
98
99 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
100 final Object externalContext) {
101
102 try {
103 final org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader, false, configLocation);
104 if (context instanceof LoggerContext) {
105 final LoggerContext ctx = (LoggerContext) context;
106 ContextAnchor.THREAD_CONTEXT.set(ctx);
107 if (externalContext != null) {
108 ctx.setExternalContext(externalContext);
109 }
110 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(name, configLocation);
111 ctx.start(config);
112 ContextAnchor.THREAD_CONTEXT.remove();
113 return ctx;
114 } else {
115 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j",
116 context.getClass().getName(), LoggerContext.class.getName());
117 }
118 } catch (final Exception ex) {
119 ex.printStackTrace();
120 }
121 return null;
122 }
123
124
125
126
127
128
129
130 public static LoggerContext initialize(final ClassLoader loader,
131 final ConfigurationFactory.ConfigurationSource source) {
132
133 try {
134 URI configLocation = null;
135 try {
136 configLocation = source.getLocation() == null ? null : new URI(source.getLocation());
137 } catch (final Exception ex) {
138
139 }
140 final org.apache.logging.log4j.spi.LoggerContext context = LogManager.getContext(loader, false, configLocation);
141 if (context instanceof LoggerContext) {
142 final LoggerContext ctx = (LoggerContext) context;
143 ContextAnchor.THREAD_CONTEXT.set(ctx);
144 final Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
145 ctx.start(config);
146 ContextAnchor.THREAD_CONTEXT.remove();
147 return ctx;
148 } else {
149 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j",
150 context.getClass().getName(), LoggerContext.class.getName());
151 }
152 } catch (final Exception ex) {
153 ex.printStackTrace();
154 }
155 return null;
156 }
157
158
159
160
161
162 public static void shutdown(final LoggerContext ctx) {
163 if (ctx != null) {
164 ctx.stop();
165 }
166 }
167 }