KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@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.cjdbc.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
32  * to 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 <code>Logger</code>.
44    *
45    * @param log4jLogger the log4j <code>Logger</code>
46    */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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