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 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation) {
51 return initialize(name, loader, configLocation, null);
52
53 }
54
55
56
57
58
59
60
61
62
63 public static LoggerContext initialize(final String name, final ClassLoader loader, final String configLocation,
64 final Object externalContext) {
65
66 try {
67 final URI uri = configLocation == null ? null : FileUtils.getCorrectedFilePathUri(configLocation);
68 return initialize(name, loader, uri, externalContext);
69 } catch (final URISyntaxException ex) {
70 LOGGER.error("There was a problem parsing the configuration location [{}].", configLocation, ex);
71 }
72 return null;
73 }
74
75
76
77
78
79
80
81 public static LoggerContext initialize(final String name, final String configLocation) {
82 return initialize(name, null, configLocation);
83 }
84
85
86
87
88
89
90
91
92 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation) {
93 return initialize(name, loader, configLocation, null);
94 }
95
96
97
98
99
100
101
102
103
104 public static LoggerContext initialize(final String name, final ClassLoader loader, final URI configLocation,
105 final Object externalContext) {
106
107 try {
108 final Log4jContextFactory factory = getFactory();
109 return factory == null ? null :
110 factory.getContext(FQCN, loader, externalContext, false, configLocation, name);
111 } catch (final Exception ex) {
112 LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].",
113 name, configLocation, ex);
114 }
115 return null;
116 }
117
118
119
120
121
122
123
124 public static LoggerContext initialize(final ClassLoader loader,
125 final ConfigurationSource source) {
126 return initialize(loader, source, null);
127 }
128
129
130
131
132
133
134
135
136
137 public static LoggerContext initialize(final ClassLoader loader,
138 final ConfigurationSource source,
139 final Object externalContext)
140 {
141
142 try {
143 final Log4jContextFactory factory = getFactory();
144 return factory == null ? null :
145 factory.getContext(FQCN, loader, externalContext, false, source);
146 } catch (final Exception ex) {
147 LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, ex);
148 }
149 return null;
150 }
151
152 private static Log4jContextFactory getFactory() {
153 final LoggerContextFactory factory = LogManager.getFactory();
154 if (factory instanceof Log4jContextFactory) {
155 return (Log4jContextFactory) factory;
156 } else if (factory != null) {
157 LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.",
158 factory.getClass().getName(), Log4jContextFactory.class.getName());
159 return null;
160 } else {
161 LOGGER.fatal("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
162 return null;
163 }
164 }
165
166
167
168
169
170 public static void shutdown(final LoggerContext ctx) {
171 if (ctx != null) {
172 ctx.stop();
173 }
174 }
175 }