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(Throwable e)
84          {
85              if (e instanceof ThreadDeath)
86                  throw (ThreadDeath)e;
87              
88              initStandardLogging(e);
89          }
90  
91          return LOG != null;
92      }
93  
94      private static void initStandardLogging(Throwable e)
95      {
96          Class<?> log_class;
97          if(e != null && __ignored)
98              e.printStackTrace();
99          if (LOG == null)
100         {
101             log_class = StdErrLog.class;
102             LOG = new StdErrLog();
103             LOG.debug("Logging to {} via {}", LOG, log_class.getName());
104         }
105     }
106 
107     public static void setLog(Logger log)
108     {
109         Log.LOG = log;
110     }
111 
112     /**
113      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
114      */
115     @Deprecated
116     public static Logger getLog()
117     {
118         initialized();
119         return LOG;
120     }
121     
122     /**
123      * Get the root logger.
124      * @return the root logger
125      */
126     public static Logger getRootLogger() {
127         initialized();
128         return LOG;
129     }
130 
131     static boolean isIgnored()
132     {
133         return __ignored;
134     }
135 
136     /**
137      * Set Log to parent Logger.
138      * <p>
139      * If there is a different Log class available from a parent classloader,
140      * call {@link #getLogger(String)} on it and construct a {@link LoggerLog} instance
141      * as this Log's Logger, so that logging is delegated to the parent Log.
142      * <p>
143      * This should be used if a webapp is using Log, but wishes the logging to be
144      * directed to the containers log.
145      * <p>
146      * If there is not parent Log, then this call is equivalent to<pre>
147      *   Log.setLog(Log.getLogger(name));
148      * </pre>
149      * @param name Logger name
150      */
151     public static void setLogToParent(String name)
152     {
153         ClassLoader loader = Log.class.getClassLoader();
154         if (loader.getParent()!=null)
155         {
156             try
157             {
158                 Class<?> uberlog = loader.getParent().loadClass("org.eclipse.jetty.util.log.Log");
159                 Method getLogger = uberlog.getMethod("getLogger", new Class[]{String.class});
160                 Object logger = getLogger.invoke(null,name);
161                 setLog(new LoggerLog(logger));
162             }
163             catch (Exception e)
164             {
165                 e.printStackTrace();
166             }
167         }
168         else
169         {
170             setLog(getLogger(name));
171         }
172     }
173 
174     /**
175      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
176      */
177     @Deprecated
178     public static void debug(Throwable th)
179     {
180         if (!isDebugEnabled())
181             return;
182         LOG.debug(EXCEPTION, th);
183     }
184 
185     /**
186      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
187      */
188     @Deprecated
189     public static void debug(String msg)
190     {
191         if (!initialized())
192             return;
193         LOG.debug(msg);
194     }
195 
196     /**
197      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
198      */
199     @Deprecated
200     public static void debug(String msg, Object arg)
201     {
202         if (!initialized())
203             return;
204         LOG.debug(msg, arg);
205     }
206 
207     /**
208      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
209      */
210     @Deprecated
211     public static void debug(String msg, Object arg0, Object arg1)
212     {
213         if (!initialized())
214             return;
215         LOG.debug(msg, arg0, arg1);
216     }
217 
218     /**
219      * Ignore an exception unless trace is enabled.
220      * This works around the problem that log4j does not support the trace level.
221      * @param thrown the Throwable to ignore
222      */
223     /**
224      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
225      */
226     @Deprecated
227     public static void ignore(Throwable thrown)
228     {
229         if (!initialized())
230             return;
231         LOG.ignore(thrown);
232     }
233 
234     /**
235      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
236      */
237     @Deprecated
238     public static void info(String msg)
239     {
240         if (!initialized())
241             return;
242         LOG.info(msg);
243     }
244 
245     /**
246      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
247      */
248     @Deprecated
249     public static void info(String msg, Object arg)
250     {
251         if (!initialized())
252             return;
253         LOG.info(msg, arg);
254     }
255 
256     /**
257      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
258      */
259     @Deprecated
260     public static void info(String msg, Object arg0, Object arg1)
261     {
262         if (!initialized())
263             return;
264         LOG.info(msg, arg0, arg1);
265     }
266 
267     /**
268      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
269      */
270     @Deprecated
271     public static boolean isDebugEnabled()
272     {
273         if (!initialized())
274             return false;
275         return LOG.isDebugEnabled();
276     }
277 
278     /**
279      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
280      */
281     @Deprecated
282     public static void warn(String msg)
283     {
284         if (!initialized())
285             return;
286         LOG.warn(msg);
287     }
288 
289     /**
290      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
291      */
292     @Deprecated
293     public static void warn(String msg, Object arg)
294     {
295         if (!initialized())
296             return;
297         LOG.warn(msg, arg);
298     }
299 
300     /**
301      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
302      */
303     @Deprecated
304     public static void warn(String msg, Object arg0, Object arg1)
305     {
306         if (!initialized())
307             return;
308         LOG.warn(msg, arg0, arg1);
309     }
310 
311     /**
312      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
313      */
314     @Deprecated
315     public static void warn(String msg, Throwable th)
316     {
317         if (!initialized())
318             return;
319         LOG.warn(msg, th);
320     }
321 
322     /**
323      * @deprecated anonymous logging is deprecated, use a named {@link Logger} obtained from {@link #getLogger(String)}
324      */
325     @Deprecated
326     public static void warn(Throwable th)
327     {
328         if (!initialized())
329             return;
330         LOG.warn(EXCEPTION, th);
331     }
332     
333     /**
334      * Obtain a named Logger based on the fully qualified class name.
335      * 
336      * @param clazz
337      *            the class to base the Logger name off of
338      * @return the Logger with the given name
339      */
340     public static Logger getLogger(Class<?> clazz)
341     {
342         return getLogger(clazz.getName());
343     }
344 
345     /**
346      * Obtain a named Logger or the default Logger if null is passed.
347      * @param name the Logger name
348      * @return the Logger with the given name
349      */
350     public static Logger getLogger(String name)
351     {
352         if (!initialized())
353             return null;
354 
355         return name == null ? LOG : LOG.getLogger(name);
356     }
357 }