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