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