1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util.log;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.reflect.Method;
24 import java.net.URL;
25 import java.security.AccessController;
26 import java.security.PrivilegedAction;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentMap;
33
34 import org.eclipse.jetty.util.Loader;
35 import org.eclipse.jetty.util.annotation.ManagedAttribute;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class Log
54 {
55 public final static String EXCEPTION= "EXCEPTION ";
56 public final static String IGNORED= "IGNORED ";
57
58
59
60
61 protected static final Properties __props;
62
63
64
65 public static String __logClass;
66
67
68
69 public static boolean __ignored;
70
71
72
73
74 private final static ConcurrentMap<String, Logger> __loggers = new ConcurrentHashMap<>();
75
76
77 static
78 {
79
80
81 __props = new Properties();
82
83 AccessController.doPrivileged(new PrivilegedAction<Object>()
84 {
85 public Object run()
86 {
87
88
89
90
91
92 URL testProps = Loader.getResource(Log.class,"jetty-logging.properties",true);
93 if (testProps != null)
94 {
95 InputStream in = null;
96 try
97 {
98 in = testProps.openStream();
99 __props.load(in);
100 }
101 catch (IOException e)
102 {
103 System.err.println("Unable to load " + testProps);
104 e.printStackTrace(System.err);
105 }
106 finally
107 {
108 safeCloseInputStream(in);
109 }
110 }
111
112
113
114
115 @SuppressWarnings("unchecked")
116 Enumeration<String> systemKeyEnum = (Enumeration<String>)System.getProperties().propertyNames();
117 while (systemKeyEnum.hasMoreElements())
118 {
119 String key = systemKeyEnum.nextElement();
120 String val = System.getProperty(key);
121
122 if (val != null)
123 __props.setProperty(key,val);
124 }
125
126
127
128 __logClass = __props.getProperty("org.eclipse.jetty.util.log.class","org.eclipse.jetty.util.log.Slf4jLog");
129 __ignored = Boolean.parseBoolean(__props.getProperty("org.eclipse.jetty.util.log.IGNORED","false"));
130 return null;
131 }
132 });
133 }
134
135 private static void safeCloseInputStream(InputStream in)
136 {
137 try
138 {
139 if (in != null)
140 in.close();
141 }
142 catch (IOException e)
143 {
144 LOG.ignore(e);
145 }
146 }
147
148 private static Logger LOG;
149 private static boolean __initialized;
150
151 public static boolean initialized()
152 {
153 if (LOG != null)
154 {
155 return true;
156 }
157
158 synchronized (Log.class)
159 {
160 if (__initialized)
161 {
162 return LOG != null;
163 }
164 __initialized = true;
165 }
166
167 try
168 {
169 Class<?> log_class = Loader.loadClass(Log.class, __logClass);
170 if (LOG == null || !LOG.getClass().equals(log_class))
171 {
172 LOG = (Logger)log_class.newInstance();
173 LOG.debug("Logging to {} via {}", LOG, log_class.getName());
174 }
175 }
176 catch(Throwable e)
177 {
178
179 initStandardLogging(e);
180 }
181
182 return LOG != null;
183 }
184
185 private static void initStandardLogging(Throwable e)
186 {
187 Class<?> log_class;
188 if(e != null && __ignored)
189 {
190 e.printStackTrace();
191 }
192
193 if (LOG == null)
194 {
195 log_class = StdErrLog.class;
196 LOG = new StdErrLog();
197 LOG.debug("Logging to {} via {}", LOG, log_class.getName());
198 }
199 }
200
201 public static Logger getLog()
202 {
203 initialized();
204 return LOG;
205 }
206
207 public static void setLog(Logger log)
208 {
209 Log.LOG = log;
210 }
211
212
213
214
215
216 public static Logger getRootLogger() {
217 initialized();
218 return LOG;
219 }
220
221 static boolean isIgnored()
222 {
223 return __ignored;
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 public static void setLogToParent(String name)
242 {
243 ClassLoader loader = Log.class.getClassLoader();
244 if (loader!=null && loader.getParent()!=null)
245 {
246 try
247 {
248 Class<?> uberlog = loader.getParent().loadClass("org.eclipse.jetty.util.log.Log");
249 Method getLogger = uberlog.getMethod("getLogger", new Class[]{String.class});
250 Object logger = getLogger.invoke(null,name);
251 setLog(new LoggerLog(logger));
252 }
253 catch (Exception e)
254 {
255 e.printStackTrace();
256 }
257 }
258 else
259 {
260 setLog(getLogger(name));
261 }
262 }
263
264
265
266
267
268
269
270
271 public static Logger getLogger(Class<?> clazz)
272 {
273 return getLogger(clazz.getName());
274 }
275
276
277
278
279
280
281 public static Logger getLogger(String name)
282 {
283 if (!initialized())
284 {
285 IllegalStateException e = new IllegalStateException();
286 e.printStackTrace();
287 throw e;
288 }
289
290 if(name==null)
291 return LOG;
292
293 Logger logger = __loggers.get(name);
294 if(logger==null)
295 logger = LOG.getLogger(name);
296
297 return logger;
298 }
299
300 static ConcurrentMap<String, Logger> getMutableLoggers()
301 {
302 return __loggers;
303 }
304
305
306
307
308
309
310 @ManagedAttribute("list of all instantiated loggers")
311 public static Map<String, Logger> getLoggers()
312 {
313 return Collections.unmodifiableMap(__loggers);
314 }
315 }