KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > log > Trace


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): __________________.
21  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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