View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.log;
15  
16  import java.lang.reflect.Method;
17  import java.security.AccessController;
18  import java.security.PrivilegedAction;
19  
20  import org.eclipse.jetty.util.Loader;
21  
22  /**
23   * Logging.
24   * This class provides a static logging interface.  If an instance of the
25   * org.slf4j.Logger class is found on the classpath, the static log methods
26   * are directed to a slf4j logger for "org.eclipse.log".   Otherwise the logs
27   * are directed to stderr.
28   * <p>
29   * The "org.eclipse.jetty.util.log.class" system property can be used
30   * to select a specific logging implementation.
31   * <p>
32   * If the system property org.eclipse.jetty.util.log.IGNORED is set,
33   * then ignored exceptions are logged in detail.
34   *
35   * @see StdErrLog
36   * @see Slf4jLog
37   */
38  public class Log
39  {
40      public final static String EXCEPTION= "EXCEPTION ";
41      public final static String IGNORED= "IGNORED ";
42  
43      public static String __logClass;
44      public static boolean __ignored;
45  
46      static
47      {
48          AccessController.doPrivileged(new PrivilegedAction<Object>()
49          {
50              public Object run()
51              {
52                  __logClass = System.getProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.Slf4jLog");
53                  __ignored = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.IGNORED", "false"));
54                  return null;
55              }
56          });
57      }
58  
59      private static Logger __log;
60      private static boolean __initialized;
61  
62      public static boolean initialized()
63      {
64          if (__log != null)
65              return true;
66  
67          synchronized (Log.class)
68          {
69              if (__initialized)
70                  return __log != null;
71              __initialized = true;
72          }
73  
74          try
75          {
76              Class<?> log_class = Loader.loadClass(Log.class, __logClass);
77              if (__log == null || !__log.getClass().equals(log_class))
78              {
79                  __log = (Logger)log_class.newInstance();
80                  __log.debug("Logging to {} via {}", __log, log_class.getName());
81              }
82          }
83          catch(NoClassDefFoundError e)
84          {
85              initStandardLogging(e);
86          }
87          catch(Exception e)
88          {
89              initStandardLogging(e);
90          }
91  
92          return __log != null;
93      }
94  
95      private static void initStandardLogging(Throwable e)
96      {
97          Class<?> log_class;
98          if(e != null && __ignored)
99              e.printStackTrace();
100         if (__log == null)
101         {
102             log_class = StdErrLog.class;
103             __log = new StdErrLog();
104             __log.debug("Logging to {} via {}", __log, log_class.getName());
105         }
106     }
107 
108     public static void setLog(Logger log)
109     {
110         Log.__log = log;
111     }
112 
113     /**
114      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
115      */
116     @Deprecated
117     public static Logger getLog()
118     {
119         initialized();
120         return __log;
121     }
122     
123     /**
124      * Get the root logger.
125      * @return the root logger
126      */
127     public static Logger getRootLogger() {
128         initialized();
129         return __log;
130     }
131 
132     /**
133      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
134      */
135     @Deprecated
136     static boolean isIgnored()
137     {
138         return __ignored;
139     }
140 
141     /**
142      * Set Log to parent Logger.
143      * <p>
144      * If there is a different Log class available from a parent classloader,
145      * call {@link #getLogger(String)} on it and construct a {@link LoggerLog} instance
146      * as this Log's Logger, so that logging is delegated to the parent Log.
147      * <p>
148      * This should be used if a webapp is using Log, but wishes the logging to be
149      * directed to the containers log.
150      * <p>
151      * If there is not parent Log, then this call is equivalent to<pre>
152      *   Log.setLog(Log.getLogger(name));
153      * </pre>
154      * @param name Logger name
155      */
156     public static void setLogToParent(String name)
157     {
158         ClassLoader loader = Log.class.getClassLoader();
159         if (loader.getParent()!=null)
160         {
161             try
162             {
163                 Class<?> uberlog = loader.getParent().loadClass("org.eclipse.jetty.util.log.Log");
164                 Method getLogger = uberlog.getMethod("getLogger", new Class[]{String.class});
165                 Object logger = getLogger.invoke(null,name);
166                 setLog(new LoggerLog(logger));
167             }
168             catch (Exception e)
169             {
170                 e.printStackTrace();
171             }
172         }
173         else
174         {
175             setLog(getLogger(name));
176         }
177     }
178 
179     /**
180      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
181      */
182     @Deprecated
183     public static void debug(Throwable th)
184     {
185         if (!isDebugEnabled())
186             return;
187         __log.debug(EXCEPTION, th);
188     }
189 
190     /**
191      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
192      */
193     @Deprecated
194     public static void debug(String msg)
195     {
196         if (!initialized())
197             return;
198         __log.debug(msg);
199     }
200 
201     /**
202      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
203      */
204     @Deprecated
205     public static void debug(String msg, Object arg)
206     {
207         if (!initialized())
208             return;
209         __log.debug(msg, arg);
210     }
211 
212     /**
213      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
214      */
215     @Deprecated
216     public static void debug(String msg, Object arg0, Object arg1)
217     {
218         if (!initialized())
219             return;
220         __log.debug(msg, arg0, arg1);
221     }
222 
223     /**
224      * Ignore an exception unless trace is enabled.
225      * This works around the problem that log4j does not support the trace level.
226      * @param thrown the Throwable to ignore
227      */
228     /**
229      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
230      */
231     @Deprecated
232     public static void ignore(Throwable thrown)
233     {
234         if (!initialized())
235             return;
236         __log.ignore(thrown);
237     }
238 
239     /**
240      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
241      */
242     @Deprecated
243     public static void info(String msg)
244     {
245         if (!initialized())
246             return;
247         __log.info(msg);
248     }
249 
250     /**
251      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
252      */
253     @Deprecated
254     public static void info(String msg, Object arg)
255     {
256         if (!initialized())
257             return;
258         __log.info(msg, arg);
259     }
260 
261     /**
262      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
263      */
264     @Deprecated
265     public static void info(String msg, Object arg0, Object arg1)
266     {
267         if (!initialized())
268             return;
269         __log.info(msg, arg0, arg1);
270     }
271 
272     /**
273      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
274      */
275     @Deprecated
276     public static boolean isDebugEnabled()
277     {
278         if (!initialized())
279             return false;
280         return __log.isDebugEnabled();
281     }
282 
283     /**
284      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
285      */
286     @Deprecated
287     public static void warn(String msg)
288     {
289         if (!initialized())
290             return;
291         __log.warn(msg);
292     }
293 
294     /**
295      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
296      */
297     @Deprecated
298     public static void warn(String msg, Object arg)
299     {
300         if (!initialized())
301             return;
302         __log.warn(msg, arg);
303     }
304 
305     /**
306      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
307      */
308     @Deprecated
309     public static void warn(String msg, Object arg0, Object arg1)
310     {
311         if (!initialized())
312             return;
313         __log.warn(msg, arg0, arg1);
314     }
315 
316     /**
317      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
318      */
319     @Deprecated
320     public static void warn(String msg, Throwable th)
321     {
322         if (!initialized())
323             return;
324         __log.warn(msg, th);
325     }
326 
327     /**
328      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
329      */
330     @Deprecated
331     public static void warn(Throwable th)
332     {
333         if (!initialized())
334             return;
335         __log.warn(EXCEPTION, th);
336     }
337     
338     /**
339      * Obtain a named Logger based on the fully qualified class name.
340      * 
341      * @param clazz
342      *            the class to base the Logger name off of
343      * @return the Logger with the given name
344      */
345     public static Logger getLogger(Class<?> clazz)
346     {
347         return getLogger(clazz.getName());
348     }
349 
350     /**
351      * Obtain a named Logger or the default Logger if null is passed.
352      * @param name the Logger name
353      * @return the Logger with the given name
354      */
355     public static Logger getLogger(String name)
356     {
357         if (!initialized())
358             return null;
359 
360         return name == null ? __log : __log.getLogger(name);
361     }
362 }