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