KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > Logger


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.logging;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 /**
29  * Logger wrapper that tries to dynamically load a log4j class to
30  * determine if log4j is available in the VM. If it is the case,
31  * a log4j delegate is built and used. In the contrary, a null
32  * logger is used. This class cannot directly reference log4j
33  * classes otherwise the JVM will try to load it and make it fail.
34  * To set
35  *
36  * <p>Only exposes the relevent factory and logging methods.
37  *
38  * <p>For JBoss the logging should be done as follows:
39  * <ul>
40  * <li>FATAL - JBoss is likely to/will crash
41  * <li>ERROR - A definite problem
42  * <li>WARN - Likely to be a problem, or it could be JBoss
43  * detected a problem it can recover from
44  * <li>INFO - Lifecycle low volume, e.g. "Bound x into jndi",
45  * things that are of interest to a user
46  * <li>DEBUG - Lifecycle low volume but necessarily of interest
47  * to the user, e.g. "Starting listener thread"
48  * <li>TRACE - High volume detailed logging
49  * </ul>
50  *
51  * @see #isTraceEnabled
52  * @see #trace(Object)
53  * @see #trace(Object,Throwable)
54  *
55  * @version <tt>$Revision: 2081 $</tt>
56  * @author Scott.Stark@jboss.org
57  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
58  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
59  */

60 public class Logger implements Serializable JavaDoc
61 {
62    /** Serialization */
63    private static final long serialVersionUID = 4232175575988879434L;
64
65    /** The system property to look for an externalized LoggerPlugin implementation class */
66    protected static String JavaDoc PLUGIN_CLASS_PROP = "org.jboss.logging.Logger.pluginClass";
67
68    /** The default LoggerPlugin implementation is log4j */
69    protected static final String JavaDoc LOG4J_PLUGIN_CLASS_NAME = "org.jboss.logging.log4j.Log4jLoggerPlugin";
70
71    /** The LoggerPlugin implementation class to use */
72    protected static Class JavaDoc pluginClass = null;
73
74    /** The class name of the LoggerPlugin implementation class to use */
75    protected static String JavaDoc pluginClassName = null;
76
77    static
78    {
79       init();
80    }
81
82    /** The logger name. */
83    private final String JavaDoc name;
84
85    /** The logger plugin delegate */
86    protected transient LoggerPlugin loggerDelegate = null;
87
88    /** The LoggerPlugin implementation class name in use
89     *
90     * @return LoggerPlugin implementation class name
91     */

92    public static String JavaDoc getPluginClassName()
93    {
94       return Logger.pluginClassName;
95    }
96
97    /**
98     * Set the LoggerPlugin implementation class name in use
99     *
100     * @param pluginClassName the LoggerPlugin implementation class name
101     */

102    public static void setPluginClassName(String JavaDoc pluginClassName)
103    {
104       if (pluginClassName.equals(Logger.pluginClassName) == false)
105       {
106          Logger.pluginClassName = pluginClassName;
107          init();
108       }
109    }
110
111    /**
112     * Creates new Logger the given logger name.
113     *
114     * @param name the logger name.
115     */

116    protected Logger(final String JavaDoc name)
117    {
118       this.name = name;
119       this.loggerDelegate = getDelegatePlugin(name);
120    }
121
122    /**
123     * Return the name of this logger.
124     *
125     * @return The name of this logger.
126     */

127    public String JavaDoc getName()
128    {
129       return name;
130    }
131
132    /**
133     * Get the logger plugin delegate
134     *
135     * @return the delegate
136     */

137    public LoggerPlugin getLoggerPlugin()
138    {
139       return this.loggerDelegate;
140    }
141
142    /**
143     * Check to see if the TRACE level is enabled for this logger.
144     *
145     * @return true if a {@link #trace(Object)} method invocation would pass
146     * the msg to the configured appenders, false otherwise.
147     */

148    public boolean isTraceEnabled()
149    {
150       return loggerDelegate.isTraceEnabled();
151    }
152
153    /**
154     * Issue a log msg with a level of TRACE.
155     *
156     * @param message the message
157     */

158    public void trace(Object JavaDoc message)
159    {
160       loggerDelegate.trace(message);
161    }
162
163    /**
164     * Issue a log msg and throwable with a level of TRACE.
165     *
166     * @param message the message
167     * @param t the throwable
168     */

169    public void trace(Object JavaDoc message, Throwable JavaDoc t)
170    {
171       loggerDelegate.trace(message, t);
172    }
173
174    /**
175     * Check to see if the DEBUG level is enabled for this logger.
176     *
177     * @deprecated DEBUG is for low volume logging, you don't need this
178     * @return true if a {@link #trace(Object)} method invocation would pass
179     * the msg to the configured appenders, false otherwise.
180     */

181    public boolean isDebugEnabled()
182    {
183       return loggerDelegate.isDebugEnabled();
184    }
185
186    /**
187     * Issue a log msg with a level of DEBUG.
188     *
189     * @param message the message
190     */

191    public void debug(Object JavaDoc message)
192    {
193       loggerDelegate.debug(message);
194    }
195
196    /**
197     * Issue a log msg and throwable with a level of DEBUG.
198     *
199     * @param message the message
200     * @param t the throwable
201     */

202    public void debug(Object JavaDoc message, Throwable JavaDoc t)
203    {
204       loggerDelegate.debug(message, t);
205    }
206
207    /**
208     * Check to see if the INFO level is enabled for this logger.
209     *
210     * @deprecated INFO is for low volume information, you don't need this
211     * @return true if a {@link #info(Object)} method invocation would pass
212     * the msg to the configured appenders, false otherwise.
213     */

214    public boolean isInfoEnabled()
215    {
216       return loggerDelegate.isInfoEnabled();
217    }
218
219    /**
220     * Issue a log msg with a level of INFO.
221     *
222     * @param message the message
223     */

224    public void info(Object JavaDoc message)
225    {
226       loggerDelegate.info(message);
227    }
228
229    /**
230     * Issue a log msg and throwable with a level of INFO.
231     *
232     * @param message the message
233     * @param t the throwable
234     */

235    public void info(Object JavaDoc message, Throwable JavaDoc t)
236    {
237       loggerDelegate.info(message, t);
238    }
239
240    /**
241     * Issue a log msg with a level of WARN.
242     *
243     * @param message the message
244     */

245    public void warn(Object JavaDoc message)
246    {
247       loggerDelegate.warn(message);
248    }
249
250    /**
251     * Issue a log msg and throwable with a level of WARN.
252     *
253     * @param message the message
254     * @param t the throwable
255     */

256    public void warn(Object JavaDoc message, Throwable JavaDoc t)
257    {
258       loggerDelegate.warn(message, t);
259    }
260
261    /**
262     * Issue a log msg with a level of ERROR.
263     *
264     * @param message the message
265     */

266    public void error(Object JavaDoc message)
267    {
268       loggerDelegate.error(message);
269    }
270
271    /**
272     * Issue a log msg and throwable with a level of ERROR.
273     *
274     * @param message the message
275     * @param t the throwable
276     */

277    public void error(Object JavaDoc message, Throwable JavaDoc t)
278    {
279       loggerDelegate.error(message, t);
280    }
281
282    /**
283     * Issue a log msg with a level of FATAL.
284     *
285     * @param message the message
286     */

287    public void fatal(Object JavaDoc message)
288    {
289       loggerDelegate.fatal(message);
290    }
291
292    /**
293     * Issue a log msg and throwable with a level of FATAL.
294     *
295     * @param message the message
296     * @param t the throwable
297     */

298    public void fatal(Object JavaDoc message, Throwable JavaDoc t)
299    {
300       loggerDelegate.fatal(message, t);
301    }
302
303    /**
304     * Custom serialization to reinitalize the delegate
305     *
306     * @param stream the object stream
307     * @throws IOException for any error
308     * @throws ClassNotFoundException if a class is not found during deserialization
309     */

310    private void readObject(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc
311    {
312       // restore non-transient fields (aka name)
313
stream.defaultReadObject();
314
315       // Restore logging
316
if (pluginClass == null)
317       {
318          init();
319       }
320       this.loggerDelegate = getDelegatePlugin(name);
321    }
322
323    /**
324     * Create a Logger instance given the logger name.
325     *
326     * @param name the logger name
327     * @return the logger
328     */

329    public static Logger getLogger(String JavaDoc name)
330    {
331       return new Logger(name);
332    }
333
334    /**
335     * Create a Logger instance given the logger name with the given suffix.
336     *
337     * <p>This will include a logger seperator between classname and suffix
338     *
339     * @param name the logger name
340     * @param suffix a suffix to append to the classname.
341     * @return the logger
342     */

343    public static Logger getLogger(String JavaDoc name, String JavaDoc suffix)
344    {
345       return new Logger(name + "." + suffix);
346    }
347
348    /**
349     * Create a Logger instance given the logger class. This simply
350     * calls create(clazz.getName()).
351     *
352     * @param clazz the Class whose name will be used as the logger name
353     * @return the logger
354     */

355    public static Logger getLogger(Class JavaDoc clazz)
356    {
357       return new Logger(clazz.getName());
358    }
359
360    /**
361     * Create a Logger instance given the logger class with the given suffix.
362     *
363     * <p>This will include a logger seperator between classname and suffix
364     *
365     * @param clazz the Class whose name will be used as the logger name.
366     * @param suffix a suffix to append to the classname.
367     * @return the logger
368     */

369    public static Logger getLogger(Class JavaDoc clazz, String JavaDoc suffix)
370    {
371       return new Logger(clazz.getName() + "." + suffix);
372    }
373
374    /**
375     * Get the delegate plugin
376     *
377     * @param name the name of the logger
378     * @return the plugin
379     */

380    protected static LoggerPlugin getDelegatePlugin(String JavaDoc name)
381    {
382       LoggerPlugin plugin = null;
383       try
384       {
385          plugin = (LoggerPlugin) pluginClass.newInstance();
386       }
387       catch (Throwable JavaDoc e)
388       {
389          plugin = new NullLoggerPlugin();
390       }
391       try
392       {
393          plugin.init(name);
394       }
395       catch (Throwable JavaDoc e)
396       {
397          String JavaDoc extraInfo = e.getMessage();
398          System.err.println("Failed to initalize plugin: " + plugin
399                + (extraInfo != null ? ", cause: " + extraInfo : ""));
400          plugin = new NullLoggerPlugin();
401       }
402
403       return plugin;
404    }
405
406    /**
407     * Initialize the LoggerPlugin class to use as the delegate to the
408     * logging system. This first checks to see if a pluginClassName has
409     * been specified via the {@link #setPluginClassName(String)} method,
410     * then the PLUGIN_CLASS_PROP system property and finally the
411     * LOG4J_PLUGIN_CLASS_NAME default. If the LoggerPlugin implementation
412     * class cannot be loaded the default NullLoggerPlugin will be used.
413     */

414    protected static void init()
415    {
416       try
417       {
418          // See if there is a PLUGIN_CLASS_PROP specified
419
if (pluginClassName == null)
420          {
421             pluginClassName = System.getProperty(PLUGIN_CLASS_PROP, LOG4J_PLUGIN_CLASS_NAME);
422          }
423
424          // Try to load the plugin via the TCL
425
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
426          pluginClass = cl.loadClass(pluginClassName);
427       }
428       catch (Throwable JavaDoc e)
429       {
430          // The plugin could not be setup, default to a null logger
431
pluginClass = org.jboss.logging.NullLoggerPlugin.class;
432       }
433    }
434 }
435
Popular Tags