1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j;
18
19 import org.apache.logging.log4j.status.StatusLogger;
20 import org.apache.logging.log4j.spi.LoggerContext;
21 import org.apache.logging.log4j.spi.LoggerContextFactory;
22
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.List;
28 import java.util.Properties;
29
30
31
32
33 public class LogManager {
34
35
36
37 public static final String ROOT_LOGGER_NAME = "";
38
39 private static final String LOGGER_RESOURCE = "META-INF/log4j-provider.xml";
40 private static final String LOGGER_CONTEXT_FACTORY = "LoggerContextFactory";
41 private static final String API_VERSION = "Log4jAPIVersion";
42 private static final String[] COMPATIBLE_API_VERSIONS = {
43 "1.99.0"
44 };
45
46 private static LoggerContextFactory factory;
47
48 private static Logger logger = StatusLogger.getLogger();
49
50
51
52
53 protected LogManager() {
54 }
55
56
57
58
59
60
61 static {
62 ClassLoader cl = findClassLoader();
63 List<LoggerContextFactory> factories = new ArrayList<LoggerContextFactory>();
64
65 Enumeration enumResources = null;
66 try {
67 enumResources = cl.getResources(LOGGER_RESOURCE);
68 } catch (IOException e) {
69 logger.fatal("Unable to locate " + LOGGER_RESOURCE, e);
70 }
71
72 if (enumResources != null) {
73 while (enumResources.hasMoreElements()) {
74 Properties props = new Properties();
75 URL url = (URL) enumResources.nextElement();
76 try {
77 props.loadFromXML(url.openStream());
78 } catch (IOException ioe) {
79 logger.error("Unable to read " + url.toString(), ioe);
80 }
81 if (!validVersion(props.getProperty(API_VERSION))) {
82 continue;
83 }
84 String className = props.getProperty(LOGGER_CONTEXT_FACTORY);
85 if (className != null) {
86 try {
87 Class clazz = cl.loadClass(className);
88 if (LoggerContextFactory.class.isAssignableFrom(clazz)) {
89 factories.add((LoggerContextFactory) clazz.newInstance());
90 } else {
91 logger.error(className + " does not implement " + LoggerContextFactory.class.getName());
92 }
93 } catch (ClassNotFoundException cnfe) {
94 logger.error("Unable to locate class " + className + " specified in " + url.toString(), cnfe);
95 } catch (IllegalAccessException iae) {
96 logger.error("Unable to create class " + className + " specified in " + url.toString(), iae);
97 } catch (Exception e) {
98 logger.error("Unable to create class " + className + " specified in " + url.toString(), e);
99 e.printStackTrace();
100 }
101 }
102 }
103 if (factories.size() != 1) {
104 logger.fatal("Unable to locate a logging implementation");
105 } else {
106 factory = factories.get(0);
107 }
108 } else {
109 logger.fatal("Unable to locate a logging implementation");
110 }
111 }
112
113
114
115
116
117 public static LoggerContextFactory getFactory() {
118 return factory;
119 }
120
121
122
123
124
125
126
127 public static Logger getLogger(String name) {
128
129 return factory.getContext(LogManager.class.getName(), false).getLogger(name);
130 }
131
132
133
134
135
136
137
138
139 protected static Logger getLogger(String fqcn, String name) {
140
141 return factory.getContext(fqcn, false).getLogger(name);
142 }
143
144
145
146
147
148
149
150
151 public static LoggerContext getContext() {
152 return factory.getContext(LogManager.class.getName(), true);
153 }
154
155
156
157
158
159
160
161
162
163
164 public static LoggerContext getContext(boolean currentContext) {
165 return factory.getContext(LogManager.class.getName(), currentContext);
166 }
167
168
169
170
171
172
173
174
175
176
177 protected static LoggerContext getContext(String fqcn, boolean currentContext) {
178 return factory.getContext(fqcn, currentContext);
179 }
180
181 private static ClassLoader findClassLoader() {
182 ClassLoader cl;
183 if (System.getSecurityManager() == null) {
184 cl = Thread.currentThread().getContextClassLoader();
185 } else {
186 cl = (ClassLoader) java.security.AccessController.doPrivileged(
187 new java.security.PrivilegedAction() {
188 public Object run() {
189 return Thread.currentThread().getContextClassLoader();
190 }
191 }
192 );
193 }
194 if (cl == null) {
195 cl = LogManager.class.getClassLoader();
196 }
197
198 return cl;
199 }
200
201 private static boolean validVersion(String version) {
202 for (String v : COMPATIBLE_API_VERSIONS) {
203 if (version.startsWith(v)) {
204 return true;
205 }
206 }
207 return false;
208 }
209
210
211
212 }