KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > common > ConsoleLog


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.common;
10
11 import org.apache.commons.logging.Log;
12
13 /**
14  * Logger sending everything to the standard output streams. This is mainly for the cases when you
15  * have a utility that does not have a logger to supply.
16  *
17  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels </a>
18  * @version CVS $Id: ConsoleLog.java,v 1.4 2003/12/14 09:53:56 benedikta Exp $
19  */

20 public final class ConsoleLog implements Log
21 {
22   public static final int TRACE = 0;
23   public static final int DEBUG = 1;
24   public static final int INFO = 2;
25   public static final int WARN = 3;
26   public static final int ERROR = 4;
27   public static final int FATAL = 5;
28   private int level = DEBUG;
29
30   public ConsoleLog() {}
31
32   public ConsoleLog(int level)
33   {
34     this.level = level;
35   }
36
37   /**
38    * <p>
39    * Log a message with trace log level.
40    * </p>
41    *
42    * @param message log this message
43    */

44   public void trace(Object JavaDoc message)
45   {
46     if (isTraceEnabled())
47     {
48       System.out.print("[TRACE] ");
49       System.out.println(message);
50     }
51   }
52
53   /**
54    * <p>
55    * Log an error with trace log level.
56    * </p>
57    *
58    * @param message log this message
59    * @param t log this cause
60    */

61   public void trace(Object JavaDoc message, Throwable JavaDoc t)
62   {
63     if (isTraceEnabled())
64     {
65       System.out.print("[TRACE] ");
66       System.out.println(message);
67       t.printStackTrace(System.out);
68     }
69   }
70
71   /**
72    * <p>
73    * Is trace logging currently enabled?
74    * </p>
75    *
76    * <p>
77    * Call this method to prevent having to perform expensive operations (for example,
78    * <code>String</code> concatination) when the log level is more than trace.
79    * </p>
80    */

81   public boolean isTraceEnabled()
82   {
83     return level<=TRACE;
84   }
85
86   /**
87    * <p>
88    * Log a message with debug log level.
89    * </p>
90    *
91    * @param message log this message
92    */

93   public void debug(Object JavaDoc message)
94   {
95     if (isDebugEnabled())
96     {
97       System.out.print("[DEBUG] ");
98       System.out.println(message);
99     }
100   }
101
102   /**
103    * <p>
104    * Log an error with debug log level.
105    * </p>
106    *
107    * @param message log this message
108    * @param t log this cause
109    */

110   public void debug(Object JavaDoc message, Throwable JavaDoc t)
111   {
112     if (isDebugEnabled())
113     {
114       System.out.print("[DEBUG] ");
115       System.out.println(message);
116       t.printStackTrace(System.out);
117     }
118   }
119
120   /**
121    * <p>
122    * Is debug logging currently enabled?
123    * </p>
124    *
125    * <p>
126    * Call this method to prevent having to perform expensive operations (for example,
127    * <code>String</code> concatination) when the log level is more than debug.
128    * </p>
129    */

130   public boolean isDebugEnabled()
131   {
132     return level<=DEBUG;
133   }
134
135   /**
136    * <p>
137    * Log a message with info log level.
138    * </p>
139    *
140    * @param message log this message
141    */

142   public void info(Object JavaDoc message)
143   {
144     if (isInfoEnabled())
145     {
146       System.out.print("[INFO] ");
147       System.out.println(message);
148     }
149   }
150
151   /**
152    * <p>
153    * Log an error with info log level.
154    * </p>
155    *
156    * @param message log this message
157    * @param t log this cause
158    */

159   public void info(Object JavaDoc message, Throwable JavaDoc t)
160   {
161     if (isInfoEnabled())
162     {
163       System.out.print("[INFO] ");
164       System.out.println(message);
165       t.printStackTrace(System.out);
166     }
167   }
168
169   /**
170    * <p>
171    * Is info logging currently enabled?
172    * </p>
173    *
174    * <p>
175    * Call this method to prevent having to perform expensive operations (for example,
176    * <code>String</code> concatination) when the log level is more than info.
177    * </p>
178    */

179   public boolean isInfoEnabled()
180   {
181     return level<=INFO;
182   }
183
184   /**
185    * <p>
186    * Log a message with warn log level.
187    * </p>
188    *
189    * @param message log this message
190    */

191   public void warn(Object JavaDoc message)
192   {
193     if (isWarnEnabled())
194     {
195       System.out.print("[WARN] ");
196       System.out.println(message);
197     }
198   }
199
200   /**
201    * <p>
202    * Log an error with warn log level.
203    * </p>
204    *
205    * @param message log this message
206    * @param t log this cause
207    */

208   public void warn(Object JavaDoc message, Throwable JavaDoc t)
209   {
210     if (isWarnEnabled())
211     {
212       System.out.print("[WARN] ");
213       System.out.println(message);
214       t.printStackTrace(System.out);
215     }
216   }
217
218   /**
219    * <p>
220    * Is warning logging currently enabled?
221    * </p>
222    *
223    * <p>
224    * Call this method to prevent having to perform expensive operations (for example,
225    * <code>String</code> concatination) when the log level is more than warning.
226    * </p>
227    */

228   public boolean isWarnEnabled()
229   {
230     return level<=WARN;
231   }
232
233   /**
234    * <p>
235    * Log a message with error log level.
236    * </p>
237    *
238    * @param message log this message
239    */

240   public void error(Object JavaDoc message)
241   {
242     if (isErrorEnabled())
243     {
244       System.out.print("[ERROR] ");
245       System.out.println(message);
246     }
247   }
248
249   /**
250    * <p>
251    * Log an error with error log level.
252    * </p>
253    *
254    * @param message log this message
255    * @param t log this cause
256    */

257   public void error(Object JavaDoc message, Throwable JavaDoc t)
258   {
259     if (isErrorEnabled())
260     {
261       System.out.print("[ERROR] ");
262       System.out.println(message);
263       t.printStackTrace(System.out);
264     }
265   }
266
267   /**
268    * <p>
269    * Is error logging currently enabled?
270    * </p>
271    *
272    * <p>
273    * Call this method to prevent having to perform expensive operations (for example,
274    * <code>String</code> concatination) when the log level is more than error.
275    * </p>
276    */

277   public boolean isErrorEnabled()
278   {
279     return level<=ERROR;
280   }
281
282   /**
283    * <p>
284    * Log a message with fatal log level.
285    * </p>
286    *
287    * @param message log this message
288    */

289   public void fatal(Object JavaDoc message)
290   {
291     if (isFatalEnabled())
292     {
293       System.out.print("[FATAL] ");
294       System.out.println(message);
295     }
296   }
297
298   /**
299    * <p>
300    * Log an error with fatal log level.
301    * </p>
302    *
303    * @param message log this message
304    * @param t log this cause
305    */

306   public void fatal(Object JavaDoc message, Throwable JavaDoc t)
307   {
308     if (isFatalEnabled())
309     {
310       System.out.print("[FATAL] ");
311       System.out.println(message);
312       t.printStackTrace(System.out);
313     }
314   }
315
316   /**
317    * <p>
318    * Is fatal logging currently enabled?
319    * </p>
320    *
321    * <p>
322    * Call this method to prevent having to perform expensive operations (for example,
323    * <code>String</code> concatination) when the log level is more than fatal.
324    * </p>
325    */

326   public boolean isFatalEnabled()
327   {
328     return level<=FATAL;
329   }
330 }
331
Popular Tags