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