KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > common > log > Trace


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): __________________.
23  */

24
25 package org.objectweb.tribe.common.log;
26
27 import org.apache.log4j.Logger;
28 import org.apache.log4j.Priority;
29
30 /**
31  * This a wrapper to the log4j logging system. We provide additional features to
32  * statically remove tracing.
33  *
34  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
35  * @version 1.0
36  */

37 public class Trace
38 {
39   /** Log4j logger instance. */
40   private Logger log4jLogger;
41
42   /**
43    * Creates a new <code>Trace</code> object from a given log4j
44    * <code>Logger</code>.
45    *
46    * @param log4jLogger the log4j <code>Logger</code>
47    */

48   protected Trace(Logger log4jLogger)
49   {
50     this.log4jLogger = log4jLogger;
51   }
52
53   /**
54    * Retrieves a logger by its name.
55    *
56    * @param name logger name
57    * @return trace a <code>Trace</code> instance
58    */

59   public static Trace getLogger(String JavaDoc name)
60   {
61     return LogManager.getLogger(name);
62   }
63
64   /**
65    * Logs a message object with the <code>DEBUG</code> <code>Level</code>.
66    *
67    * @param message the message object to log
68    */

69   public void debug(Object JavaDoc message)
70   {
71     log4jLogger.debug(message);
72   }
73
74   /**
75    * Logs a message object with the <code>DEBUG</code> <code>Level</code>
76    * including the stack trace of the {@link Throwable}<code>error</code>
77    * passed as parameter.
78    *
79    * @param message the message object to log
80    * @param error the exception to log, including its stack trace
81    */

82   public void debug(Object JavaDoc message, Throwable JavaDoc error)
83   {
84     log4jLogger.debug(message, error);
85   }
86
87   /**
88    * Logs a message object with the <code>ERROR</code> <code>Level</code>.
89    *
90    * @param message the message object to log
91    */

92   public void error(Object JavaDoc message)
93   {
94     log4jLogger.error(message);
95   }
96
97   /**
98    * Logs a message object with the <code>ERROR</code> <code>Level</code>
99    * including the stack trace of the {@link Throwable}<code>error</code>
100    * passed as parameter.
101    *
102    * @param message the message object to log.
103    * @param error the exception to log, including its stack trace.
104    */

105   public void error(Object JavaDoc message, Throwable JavaDoc error)
106   {
107     log4jLogger.error(message, error);
108   }
109
110   /**
111    * Logs a message object with the <code>FATAL</code> <code>Level</code>.
112    *
113    * @param message the message object to log.
114    */

115   public void fatal(Object JavaDoc message)
116   {
117     log4jLogger.fatal(message);
118   }
119
120   /**
121    * Logs a message object with the <code>FATAL</code> <code>Level</code>
122    * including the stack trace of the {@link Throwable}<code>error</code>
123    * passed as parameter.
124    *
125    * @param message the message object to log.
126    * @param error the exception to log, including its stack trace.
127    */

128   public void fatal(Object JavaDoc message, Throwable JavaDoc error)
129   {
130     log4jLogger.fatal(message, error);
131   }
132
133   /**
134    * Logs a message object with the <code>INFO</code> <code>Level</code>.
135    *
136    * @param message the message object to log.
137    */

138   public void info(Object JavaDoc message)
139   {
140     log4jLogger.info(message);
141   }
142
143   /**
144    * Logs a message object with the <code>INFO</code> <code>Level</code>
145    * including the stack trace of the {@link Throwable}<code>error</code>
146    * passed as parameter.
147    *
148    * @param message the message object to log.
149    * @param error the exception to log, including its stack trace.
150    */

151   public void info(Object JavaDoc message, Throwable JavaDoc error)
152   {
153     log4jLogger.info(message, error);
154   }
155
156   /**
157    * Logs a message object with the <code>WARN</code> <code>Level</code>.
158    *
159    * @param message the message object to log.
160    */

161   public void warn(Object JavaDoc message)
162   {
163     log4jLogger.warn(message);
164   }
165
166   /**
167    * Logs a message object with the <code>WARN</code> <code>Level</code>
168    * including the stack trace of the {@link Throwable}<code>error</code>
169    * passed as parameter.
170    *
171    * @param message the message object to log.
172    * @param error the exception to log, including its stack trace.
173    */

174   public void warn(Object JavaDoc message, Throwable JavaDoc error)
175   {
176     log4jLogger.warn(message, error);
177   }
178
179   /**
180    * Checks whether this category is enabled for the
181    * <code>DEBUG</code> <code>Level</code>.
182    * <p>
183    * This function is intended to lessen the computational cost of disabled log
184    * debug statements.
185    * <p>
186    * For some <code>cat</code> Category object, when you write,
187    *
188    * <pre>
189    * cat.debug("This is entry number: " + i );
190    * </pre>
191    *
192    * <p>
193    * You incur the cost constructing the message, concatenatiion in this case,
194    * regardless of whether the message is logged or not.
195    * <p>
196    * If you are worried about speed, then you should write
197    *
198    * <pre>
199    * if(cat.isDebugEnabled()) { cat.debug("This is entry number: " + i ); }
200    * </pre>
201    *
202    * <p>
203    * This way you will not incur the cost of parameter construction if debugging
204    * is disabled for <code>cat</code>. On the other hand, if the
205    * <code>cat</code> is debug enabled, you will incur the cost of evaluating
206    * whether the category is debug enabled twice. Once in
207    * <code>isDebugEnabled</code> and once in the <code>debug</code>. This
208    * is an insignificant overhead since evaluating a category takes about 1%% of
209    * the time it takes to actually log.
210    *
211    * @return <code>true</code> if this category is debug enabled,
212    * <code>false</code> otherwise.
213    */

214   public boolean isDebugEnabled()
215   {
216     return log4jLogger.isDebugEnabled();
217   }
218
219   /**
220    * Checks whether this category is enabled for the <code>INFO</code>
221    * <code>Level</code>.
222    * See also {@link #isDebugEnabled}.
223    *
224    * @return <code>true</code> if this category is enabled for
225    * <code>Level</code> <code>INFO</code>,<code>false</code>
226    * otherwise.
227    */

228   public boolean isInfoEnabled()
229   {
230     return log4jLogger.isInfoEnabled();
231   }
232
233   /**
234    * Checks whether this category is enabled for the <code>INFO</code>
235    * <code>Level</code>.
236    * See also {@link #isDebugEnabled}.
237    *
238    * @return <code>true</code> if this category is enabled for
239    * <code>INFO</code> <code>Level</code>,<code>false</code>
240    * otherwise.
241    */

242   public boolean isWarnEnabled()
243   {
244     return log4jLogger.isEnabledFor(Priority.WARN);
245   }
246
247   /**
248    * Checks whether this category is enabled for the <code>INFO</code>
249    * <code>Level</code>.
250    * See also {@link #isDebugEnabled}.
251    *
252    * @return <code>true</code> if this category is enabled for
253    * <code>INFO</code> <code>Level</code>,<code>false</code>
254    * otherwise.
255    */

256   public boolean isErrorEnabled()
257   {
258     return log4jLogger.isEnabledFor(Priority.ERROR);
259   }
260
261   /**
262    * Checks whether this category is enabled for the <code>INFO</code>
263    * <code>Level</code>.
264    * See also {@link #isDebugEnabled}.
265    *
266    * @return <code>true</code> if this category is enabled for
267    * <code>INFO</code> <code>Level</code>,<code>false</code>
268    * otherwise.
269    */

270   public boolean isFatalEnabled()
271   {
272     return log4jLogger.isEnabledFor(Priority.FATAL);
273   }
274 }
Popular Tags