KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > discovery > log > SimpleLog


1 /*
2  * $Header: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/impl/SimpleLog.java,v 1.4 2002/06/15 20:54:48 craigmcc Exp $
3  * $Revision: 1.4 $
4  * $Date: 2002/06/15 20:54:48 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62
63 package org.apache.commons.discovery.log;
64
65 import java.io.PrintStream JavaDoc;
66 import java.text.DateFormat JavaDoc;
67 import java.text.SimpleDateFormat JavaDoc;
68 import java.util.Date JavaDoc;
69
70 import org.apache.commons.logging.Log;
71
72
73 /**
74  * <p>Simple implementation of Log that sends all enabled log messages,
75  * for all defined loggers, to System.err.
76  * </p>
77  *
78  * <p>Hacked from commons-logging SimpleLog for use in discovery.
79  * This is intended to be enough of a Log implementation to bootstrap
80  * Discovery.
81  * </p>
82  *
83  * <p>One property: <code>org.apache.commons.discovery.log.level</code>.
84  * valid values: all, trace, debug, info, warn, error, fatal, off.
85  * </p>
86  *
87  * @author Richard A. Sitze
88  * @author <a HREF="mailto:sanders@apache.org">Scott Sanders</a>
89  * @author Rod Waldhoff
90  * @author Robert Burrell Donkin
91  *
92  * @version $Id: SimpleLog.java,v 1.4 2002/06/15 20:54:48 craigmcc Exp $
93  */

94 public class SimpleLog implements Log {
95       // ---------------------------------------------------- Log Level Constants
96

97     /** "Trace" level logging. */
98     public static final int LOG_LEVEL_TRACE = 1;
99     /** "Debug" level logging. */
100     public static final int LOG_LEVEL_DEBUG = 2;
101     /** "Info" level logging. */
102     public static final int LOG_LEVEL_INFO = 3;
103     /** "Warn" level logging. */
104     public static final int LOG_LEVEL_WARN = 4;
105     /** "Error" level logging. */
106     public static final int LOG_LEVEL_ERROR = 5;
107     /** "Fatal" level logging. */
108     public static final int LOG_LEVEL_FATAL = 6;
109
110     /** Enable all logging levels */
111     public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1);
112
113     /** Enable no logging levels */
114     public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1);
115
116     // ------------------------------------------------------- Class Attributes
117

118     static protected final String JavaDoc PROP_LEVEL =
119         "org.apache.commons.discovery.log.level";
120
121     /** Include the instance name in the log message? */
122     static protected boolean showLogName = false;
123
124     /** Include the short name ( last component ) of the logger in the log
125         message. Default to true - otherwise we'll be lost in a flood of
126         messages without knowing who sends them.
127     */

128     static protected boolean showShortName = true;
129
130     /** Include the current time in the log message */
131     static protected boolean showDateTime = false;
132
133     /** Used to format times */
134     static protected DateFormat JavaDoc dateFormatter = null;
135
136     /** The current log level */
137     static protected int logLevel = LOG_LEVEL_INFO;
138     
139     
140     /**
141      * Use 'out' instead of 'err' for logging
142      * to keep in-sync with test messages.
143      */

144     static private PrintStream JavaDoc out = System.out;
145
146     // ------------------------------------------------------------ Initializer
147

148     // initialize class attributes
149
static {
150         if(showDateTime) {
151             dateFormatter = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss:SSS zzz");
152         }
153
154         // set log level from properties
155
String JavaDoc lvl = System.getProperty(PROP_LEVEL);
156
157         if("all".equalsIgnoreCase(lvl)) {
158             setLevel(SimpleLog.LOG_LEVEL_ALL);
159         } else if("trace".equalsIgnoreCase(lvl)) {
160             setLevel(SimpleLog.LOG_LEVEL_TRACE);
161         } else if("debug".equalsIgnoreCase(lvl)) {
162             setLevel(SimpleLog.LOG_LEVEL_DEBUG);
163         } else if("info".equalsIgnoreCase(lvl)) {
164             setLevel(SimpleLog.LOG_LEVEL_INFO);
165         } else if("warn".equalsIgnoreCase(lvl)) {
166             setLevel(SimpleLog.LOG_LEVEL_WARN);
167         } else if("error".equalsIgnoreCase(lvl)) {
168             setLevel(SimpleLog.LOG_LEVEL_ERROR);
169         } else if("fatal".equalsIgnoreCase(lvl)) {
170             setLevel(SimpleLog.LOG_LEVEL_FATAL);
171         } else if("off".equalsIgnoreCase(lvl)) {
172             setLevel(SimpleLog.LOG_LEVEL_OFF);
173         }
174     }
175
176     // -------------------------------------------------------- Properties
177

178     /**
179      * <p> Set logging level. </p>
180      *
181      * @param level new logging level
182      */

183     public static void setLevel(int currentLogLevel) {
184         logLevel = currentLogLevel;
185     }
186
187     /**
188      * <p> Get logging level. </p>
189      */

190     public static int getLevel() {
191         return logLevel;
192     }
193
194     /**
195      * Is the given log level currently enabled?
196      *
197      * @param logLevel is this level enabled?
198      */

199     protected static boolean isLevelEnabled(int level) {
200         // log level are numerically ordered so can use simple numeric
201
// comparison
202
return (level >= getLevel());
203     }
204
205
206
207     // ------------------------------------------------------------- Attributes
208

209     /** The name of this simple log instance */
210     protected String JavaDoc logName = null;
211
212     private String JavaDoc prefix=null;
213
214     
215     // ------------------------------------------------------------ Constructor
216

217     /**
218      * Construct a simple log with given name.
219      *
220      * @param name log name
221      */

222     public SimpleLog(String JavaDoc name) {
223         logName = name;
224     }
225
226
227     // -------------------------------------------------------- Logging Methods
228

229
230     /**
231      * <p> Do the actual logging.
232      * This method assembles the message
233      * and then prints to <code>System.err</code>.</p>
234      */

235     protected void log(int type, Object JavaDoc message, Throwable JavaDoc t) {
236         // use a string buffer for better performance
237
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
238
239         // append date-time if so configured
240
if(showDateTime) {
241             buf.append(dateFormatter.format(new Date JavaDoc()));
242             buf.append(" ");
243         }
244
245         // append a readable representation of the log leve
246
switch(type) {
247             case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break;
248             case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break;
249             case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO ] "); break;
250             case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN ] "); break;
251             case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break;
252             case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break;
253         }
254
255         // append the name of the log instance if so configured
256
if( showShortName) {
257             if( prefix==null ) {
258                 // cut all but the last component of the name for both styles
259
prefix = logName.substring( logName.lastIndexOf(".") +1) + " - ";
260                 prefix = prefix.substring( prefix.lastIndexOf("/") +1) + "-";
261             }
262             buf.append( prefix );
263         } else if(showLogName) {
264             buf.append(String.valueOf(logName)).append(" - ");
265         }
266
267         // append the message
268
buf.append(String.valueOf(message));
269
270         // append stack trace if not null
271
if(t != null) {
272             buf.append(" <");
273             buf.append(t.toString());
274             buf.append(">");
275         }
276
277         // print to System.err
278
out.println(buf.toString());
279         
280         if (t != null)
281             t.printStackTrace(System.err);
282     }
283
284
285     // -------------------------------------------------------- Log Implementation
286

287
288     /**
289      * <p> Log a message with debug log level.</p>
290      */

291     public final void debug(Object JavaDoc message) {
292         if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
293             log(SimpleLog.LOG_LEVEL_DEBUG, message, null);
294         }
295     }
296
297
298     /**
299      * <p> Log an error with debug log level.</p>
300      */

301     public final void debug(Object JavaDoc message, Throwable JavaDoc t) {
302         if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) {
303             log(SimpleLog.LOG_LEVEL_DEBUG, message, t);
304         }
305     }
306
307
308     /**
309      * <p> Log a message with debug log level.</p>
310      */

311     public final void trace(Object JavaDoc message) {
312         if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
313             log(SimpleLog.LOG_LEVEL_TRACE, message, null);
314         }
315     }
316
317
318     /**
319      * <p> Log an error with debug log level.</p>
320      */

321     public final void trace(Object JavaDoc message, Throwable JavaDoc t) {
322         if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) {
323             log(SimpleLog.LOG_LEVEL_TRACE, message, t);
324         }
325     }
326
327
328     /**
329      * <p> Log a message with info log level.</p>
330      */

331     public final void info(Object JavaDoc message) {
332         if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
333             log(SimpleLog.LOG_LEVEL_INFO,message,null);
334         }
335     }
336
337
338     /**
339      * <p> Log an error with info log level.</p>
340      */

341     public final void info(Object JavaDoc message, Throwable JavaDoc t) {
342         if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) {
343             log(SimpleLog.LOG_LEVEL_INFO, message, t);
344         }
345     }
346
347
348     /**
349      * <p> Log a message with warn log level.</p>
350      */

351     public final void warn(Object JavaDoc message) {
352         if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
353             log(SimpleLog.LOG_LEVEL_WARN, message, null);
354         }
355     }
356
357
358     /**
359      * <p> Log an error with warn log level.</p>
360      */

361     public final void warn(Object JavaDoc message, Throwable JavaDoc t) {
362         if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) {
363             log(SimpleLog.LOG_LEVEL_WARN, message, t);
364         }
365     }
366
367
368     /**
369      * <p> Log a message with error log level.</p>
370      */

371     public final void error(Object JavaDoc message) {
372         if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
373             log(SimpleLog.LOG_LEVEL_ERROR, message, null);
374         }
375     }
376
377
378     /**
379      * <p> Log an error with error log level.</p>
380      */

381     public final void error(Object JavaDoc message, Throwable JavaDoc t) {
382         if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) {
383             log(SimpleLog.LOG_LEVEL_ERROR, message, t);
384         }
385     }
386
387
388     /**
389      * <p> Log a message with fatal log level.</p>
390      */

391     public final void fatal(Object JavaDoc message) {
392         if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
393             log(SimpleLog.LOG_LEVEL_FATAL, message, null);
394         }
395     }
396
397
398     /**
399      * <p> Log an error with fatal log level.</p>
400      */

401     public final void fatal(Object JavaDoc message, Throwable JavaDoc t) {
402         if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) {
403             log(SimpleLog.LOG_LEVEL_FATAL, message, t);
404         }
405     }
406
407
408     /**
409      * <p> Are debug messages currently enabled? </p>
410      *
411      * <p> This allows expensive operations such as <code>String</code>
412      * concatenation to be avoided when the message will be ignored by the
413      * logger. </p>
414      */

415     public final boolean isDebugEnabled() {
416         return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG);
417     }
418
419
420     /**
421      * <p> Are error messages currently enabled? </p>
422      *
423      * <p> This allows expensive operations such as <code>String</code>
424      * concatenation to be avoided when the message will be ignored by the
425      * logger. </p>
426      */

427     public final boolean isErrorEnabled() {
428         return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR);
429     }
430
431
432     /**
433      * <p> Are fatal messages currently enabled? </p>
434      *
435      * <p> This allows expensive operations such as <code>String</code>
436      * concatenation to be avoided when the message will be ignored by the
437      * logger. </p>
438      */

439     public final boolean isFatalEnabled() {
440         return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL);
441     }
442
443
444     /**
445      * <p> Are info messages currently enabled? </p>
446      *
447      * <p> This allows expensive operations such as <code>String</code>
448      * concatenation to be avoided when the message will be ignored by the
449      * logger. </p>
450      */

451     public final boolean isInfoEnabled() {
452         return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO);
453     }
454
455
456     /**
457      * <p> Are trace messages currently enabled? </p>
458      *
459      * <p> This allows expensive operations such as <code>String</code>
460      * concatenation to be avoided when the message will be ignored by the
461      * logger. </p>
462      */

463     public final boolean isTraceEnabled() {
464         return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE);
465     }
466
467
468     /**
469      * <p> Are warn messages currently enabled? </p>
470      *
471      * <p> This allows expensive operations such as <code>String</code>
472      * concatenation to be avoided when the message will be ignored by the
473      * logger. </p>
474      */

475     public final boolean isWarnEnabled() {
476         return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN);
477     }
478 }
479
Popular Tags