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