KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > log > LogImpl


1 // ========================================================================
2
// Copyright (c) 1997 MortBay Consulting, Sydney
3
// $Id: LogImpl.java,v 1.6 2005/08/13 00:01:27 gregwilkins Exp $
4
// ========================================================================
5

6 package org.mortbay.log;
7 import java.util.ArrayList JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.mortbay.util.Loader;
11
12 /*-----------------------------------------------------------------------*/
13 /** A Commons Log implementation for Jetty logs.
14  *
15  * The log can contain multiple log syncs.
16  * The following system properties can be used to control the configuration:<pre>
17  * DEBUG - if set debugging is output is enabled.
18  * DEBUG_PATTERNS - A list of substring patterns used to match against log information for
19  * fine grained control of debug logging.
20  * DEBUG_VERBOSE - If set to a positive integer, trace and info are enabled.
21  * If set to zero, then info is enabled.
22  * LOG_SINKS - List of class names used to instantiate the log sinks.
23  * </pre>
24  * This logger can be configured with the org.mortbay.log.Factory
25  *
26  * @see org.mortbay.log.LogFactory
27  */

28 public class LogImpl implements org.apache.commons.logging.Log
29 {
30     /*-------------------------------------------------------------------*/
31     public final static String JavaDoc DEBUG= "DEBUG ";
32     public final static String JavaDoc INFO= "INFO ";
33     public final static String JavaDoc TRACE= "TRACE ";
34     public final static String JavaDoc FAIL= "FAIL!! ";
35     public final static String JavaDoc WARN= "WARN!! ";
36     public final static String JavaDoc ERROR= "ERROR! ";
37
38     /*-------------------------------------------------------------------*/
39     boolean _debugOn=false;
40     private ArrayList JavaDoc _debugPatterns=null;
41     private boolean _initialized = false;
42     private String JavaDoc _patterns=null;
43
44     
45     /*-------------------------------------------------------------------*/
46     public LogSink[] _sinks = null;
47     private boolean _suppressWarnings=false;
48     private int _verbose=0;
49     
50     /*-------------------------------------------------------------------*/
51     /** Construct the shared instance of Log that decodes the
52      * options setup in the environments properties.
53      */

54     public LogImpl()
55     {
56         try{
57             _debugOn= System.getProperty("DEBUG") != null;
58             setDebugPatterns(System.getProperty("DEBUG_PATTERNS"));
59             setVerbose(Integer.getInteger("DEBUG_VERBOSE",0).intValue());
60         }
61         catch (Exception JavaDoc e)
62         {
63             System.err.println("Exception from getProperty!\n"+
64                                "Probably running in applet\n"+
65                                "Use Code.initParamsFromApplet or Code.setOption to control debug output.");
66         }
67     }
68     
69     /* ------------------------------------------------------------ */
70     /** Add a Log Sink.
71      * @param logSink
72      */

73     public synchronized void add(LogSink logSink)
74         throws Exception JavaDoc
75     {
76         logSink.setLogImpl(this);
77         if (!logSink.isStarted())
78             logSink.start();
79         
80         if (_sinks==null)
81         {
82             _sinks=new LogSink[1];
83             _sinks[0]=logSink;
84         }
85         else
86         {
87             boolean slotFree = false;
88             for( int i=_sinks.length; i-->0; )
89             {
90                 if( _sinks[i] == null )
91                 {
92                     slotFree = true;
93                     _sinks[i] = logSink;
94                     break;
95                 }
96             }
97
98             if( !slotFree )
99             {
100                 LogSink[] ns = new LogSink[_sinks.length+1];
101                 for (int i=_sinks.length;i-->0;)
102                     ns[i]=_sinks[i];
103                 ns[_sinks.length]=logSink;
104                 _sinks=ns;
105             }
106         }
107         _initialized = true;
108
109         info("added "+logSink);
110     }
111
112
113     /* ------------------------------------------------------------ */
114     /** Add a Log Sink.
115      * @param logSinkClass The logsink classname or null for the default.
116      */

117     public synchronized void add(String JavaDoc logSinkClass)
118     {
119         try
120         {
121             if (logSinkClass==null || logSinkClass.length()==0)
122                 logSinkClass="org.mortbay.log.OutputStreamLogSink";
123             Class JavaDoc sinkClass = Loader.loadClass(this.getClass(),logSinkClass);
124             LogSink sink=(LogSink)sinkClass.newInstance();
125             add(sink);
126         }
127         catch(Exception JavaDoc e)
128         {
129             message(WARN,e,2);
130             throw new IllegalArgumentException JavaDoc(e.toString());
131         }
132         
133     }
134
135     /* (non-Javadoc)
136      * @see org.apache.commons.logging.Log#debug(java.lang.Object)
137      */

138     public void debug(Object JavaDoc m)
139     {
140         if (_debugOn)
141         {
142             Frame frame = new Frame(1,true);
143             if (isDebugOnFor(frame))
144             {
145                 frame.complete();
146                 message(DEBUG,m,frame);
147             }
148         }
149     }
150
151     /* (non-Javadoc)
152      * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
153      */

154     public void debug(Object JavaDoc m, Throwable JavaDoc ex)
155     {
156         if (_debugOn)
157         {
158             Frame frame = new Frame(1,true);
159             if (isDebugOnFor(frame))
160             {
161                 frame.complete();
162                 message(DEBUG,new Object JavaDoc[]{m,ex},frame);
163             }
164         }
165     }
166     
167     
168     /*-------------------------------------------------------------------*/
169     /** Default initialization is used the first time we have to log
170      * unless a sink has been added with add(). _needInit allows us to
171      * distinguish between initial state and disabled state.
172      */

173     private synchronized void defaultInit()
174     {
175         if (!_initialized)
176         {
177             _initialized = true;
178             String JavaDoc sinkClasses = System.getProperty("LOG_SINKS","org.mortbay.log.OutputStreamLogSink");
179             StringTokenizer JavaDoc sinkTokens = new StringTokenizer JavaDoc(sinkClasses, ";, ");
180                     
181             LogSink sink= null;
182             while (sinkTokens.hasMoreTokens())
183             {
184                 String JavaDoc sinkClassName = sinkTokens.nextToken();
185                         
186                 try
187                 {
188                     Class JavaDoc sinkClass = Loader.loadClass(this.getClass(),sinkClassName);
189                     if (org.mortbay.log.LogSink.class.isAssignableFrom(sinkClass)) {
190                         sink = (LogSink)sinkClass.newInstance();
191                         sink.start();
192                         add(sink);
193                     }
194                     else
195                         // Can't use Code.fail here, that's what we're setting up
196
System.err.println(sinkClass+" is not a org.mortbay.log.LogSink");
197                 }
198                 catch (Exception JavaDoc e) {
199                     e.printStackTrace();
200                 }
201             }
202         }
203     }
204     
205     /* ------------------------------------------------------------ */
206     /**
207      */

208     public synchronized void deleteStoppedLogSinks()
209     {
210         if (_sinks!=null)
211         {
212             for (int s=_sinks.length;s-->0;)
213             {
214                 if (_sinks[s]==null)
215                     continue;
216                 if (!_sinks[s].isStarted())
217                     _sinks[s]=null;
218             }
219         }
220     }
221     
222     /* ------------------------------------------------------------ */
223     /** No logging.
224      * All log sinks are stopped and removed.
225      */

226     public synchronized void reset()
227     {
228         info("reset");
229         if (_sinks!=null) {
230             for (int s=_sinks.length;s-->0;)
231             {
232                 try{
233                     if (_sinks[s]!=null)
234                         _sinks[s].stop();
235                     _sinks[s]=null;
236                 }
237                 catch(InterruptedException JavaDoc e)
238                 {
239                     if (getDebug() && getVerbose()>0)
240                         message("WARN",e);
241                 }
242             }
243             _sinks=null;
244         }
245         _initialized=true;
246     }
247
248     
249     /* (non-Javadoc)
250      * @see org.apache.commons.logging.Log#error(java.lang.Object)
251      */

252     public void error(Object JavaDoc arg0)
253     {
254         message(ERROR,arg0,new Frame(1));
255         
256     }
257
258     /* (non-Javadoc)
259      * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
260      */

261     public void error(Object JavaDoc arg0, Throwable JavaDoc arg1)
262     {
263         message(ERROR,new Object JavaDoc[]{arg0,arg1},new Frame(1));
264     }
265
266     /* (non-Javadoc)
267      * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
268      */

269     public void fatal(Object JavaDoc arg0)
270     {
271         message(FAIL,arg0,new Frame(1));
272         
273     }
274
275     /* (non-Javadoc)
276      * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
277      */

278     public void fatal(Object JavaDoc arg0, Throwable JavaDoc arg1)
279     {
280         message(FAIL,new Object JavaDoc[]{arg0,arg1},new Frame(1));
281     }
282     
283     
284     /* ------------------------------------------------------------ */
285     /** Get the debug status.
286      * @return the debug status
287      */

288     public boolean getDebug()
289     {
290         return _debugOn;
291     }
292
293     /* ------------------------------------------------------------ */
294     /** Get the debug patterns.
295      * @return Coma separated list of debug patterns
296      */

297     public String JavaDoc getDebugPatterns()
298     {
299         return _patterns;
300     }
301     
302     /* ------------------------------------------------------------ */
303     public LogSink[] getLogSinks()
304     {
305         return _sinks;
306     }
307
308     /* ------------------------------------------------------------ */
309     /** Get the warnings suppression status.
310      * @return the warnings suppression status
311      */

312     public boolean getSuppressWarnings()
313     {
314         return _suppressWarnings;
315     }
316
317     /* ------------------------------------------------------------ */
318     /** Get the verbosity level.
319      * @return the verbosity level
320      */

321     public int getVerbose()
322     {
323         return _verbose;
324     }
325
326     /* (non-Javadoc)
327      * @see org.apache.commons.logging.Log#info(java.lang.Object)
328      */

329     public void info(Object JavaDoc arg0)
330     {
331         if (isInfoEnabled())
332             message(INFO,arg0,new Frame(1));
333         
334     }
335
336     /* (non-Javadoc)
337      * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
338      */

339     public void info(Object JavaDoc arg0, Throwable JavaDoc arg1)
340     {
341         if (isInfoEnabled())
342             message(INFO,new Object JavaDoc[]{arg0,arg1},new Frame(1));
343     }
344
345     /* (non-Javadoc)
346      * @see org.apache.commons.logging.Log#isDebugEnabled()
347      */

348     public boolean isDebugEnabled()
349     {
350         return _debugOn;
351     }
352
353     /* (non-Javadoc)
354      * @see org.apache.commons.logging.Log#isErrorEnabled()
355      */

356     public boolean isErrorEnabled()
357     {
358         return !_suppressWarnings;
359     }
360
361     /* (non-Javadoc)
362      * @see org.apache.commons.logging.Log#isFatalEnabled()
363      */

364     public boolean isFatalEnabled()
365     {
366         return true;
367     }
368
369     /* (non-Javadoc)
370      * @see org.apache.commons.logging.Log#isInfoEnabled()
371      */

372     public boolean isInfoEnabled()
373     {
374         return _verbose>=0;
375     }
376
377     /* (non-Javadoc)
378      * @see org.apache.commons.logging.Log#isTraceEnabled()
379      */

380     public boolean isTraceEnabled()
381     {
382         return _verbose>0;
383     }
384
385     /* (non-Javadoc)
386      * @see org.apache.commons.logging.Log#isWarnEnabled()
387      */

388     public boolean isWarnEnabled()
389     {
390         return !_suppressWarnings;
391     }
392     
393     /*-------------------------------------------------------------------*/
394     public void message(String JavaDoc tag,
395                         Object JavaDoc msg,
396                         Frame frame)
397     {
398         long time = System.currentTimeMillis();
399         message(tag,msg,frame,time);
400     }
401     
402     /* ------------------------------------------------------------ */
403     /** Log a message.
404      * @param tag Tag for type of log
405      * @param msg The message
406      * @param frame The frame that generated the message.
407      * @param time The time stamp of the message.
408      */

409     public synchronized void message(String JavaDoc tag,
410                                      Object JavaDoc msg,
411                                      Frame frame,
412                                      long time)
413     {
414         if (!_initialized)
415             defaultInit();
416         
417         boolean logged=false;
418         if (_sinks!=null)
419         {
420             for (int s=_sinks.length;s-->0;)
421             {
422                 if (_sinks[s]==null)
423                     continue;
424             
425                 if (_sinks[s].isStarted())
426                 {
427                     logged=true;
428                     _sinks[s].log(tag,msg,frame,time);
429                 }
430             }
431         }
432
433         if (!logged)
434             System.err.println(time+": "+tag+":"+msg+" @ "+frame);
435     }
436     
437     /* ------------------------------------------------------------ */
438     /** Log a message.
439      * @param tag Tag for type of log
440      * @param msg The message
441      */

442     public synchronized void message(String JavaDoc tag,
443                                      Object JavaDoc msg)
444     {
445         message(tag,msg,new Frame(1),System.currentTimeMillis());
446     }
447     
448     /* ------------------------------------------------------------ */
449     /** Log a message.
450      * @param tag Tag for type of log
451      * @param msg The message
452      */

453     public synchronized void message(String JavaDoc tag,
454                                      Object JavaDoc msg,
455                                      int depth)
456     {
457         message(tag,msg,new Frame(depth),System.currentTimeMillis());
458     }
459
460     /* ------------------------------------------------------------ */
461     /** Set if debugging is on or off.
462      * @param debug
463      */

464     public synchronized void setDebug(boolean debug)
465     {
466         boolean oldDebug=_debugOn;
467         if (_debugOn && !debug)
468             this.message(DEBUG,"DEBUG OFF");
469         _debugOn=debug;
470         if (!oldDebug && debug)
471             this.message(DEBUG,"DEBUG ON");
472     }
473     
474     /* ------------------------------------------------------------ */
475     /** Set debug patterns.
476      * @param patterns comma separated string of patterns
477      */

478     public void setDebugPatterns(String JavaDoc patterns)
479     {
480         _patterns=patterns;
481         if (patterns!=null && patterns.length()>0)
482         {
483             _debugPatterns = new ArrayList JavaDoc();
484
485             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(patterns,", \t");
486             while (tok.hasMoreTokens())
487             {
488                 String JavaDoc pattern = tok.nextToken();
489                 _debugPatterns.add(pattern);
490             }
491         }
492         else
493             _debugPatterns = null;
494     }
495
496     /* ------------------------------------------------------------ */
497     /** Set warning suppression.
498      * @param warnings Warnings suppress if this is true and debug is false
499      */

500     public void setSuppressWarnings(boolean warnings)
501     {
502         _suppressWarnings=warnings;
503     }
504
505     
506     /* ------------------------------------------------------------ */
507     /** Set verbosity level.
508      * @param verbose
509      */

510     public void setVerbose(int verbose)
511     {
512         _verbose=verbose;
513     }
514
515     /* (non-Javadoc)
516      * @see org.apache.commons.logging.Log#trace(java.lang.Object)
517      */

518     public void trace(Object JavaDoc arg0)
519     {
520         if (isTraceEnabled())
521             message(TRACE,arg0,new Frame(1));
522     }
523
524     /* (non-Javadoc)
525      * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
526      */

527     public void trace(Object JavaDoc arg0, Throwable JavaDoc arg1)
528     {
529         if (isTraceEnabled())
530             message(TRACE,new Object JavaDoc[]{arg0,arg1},new Frame(1));
531     }
532
533     /* (non-Javadoc)
534      * @see org.apache.commons.logging.Log#warn(java.lang.Object)
535      */

536     public void warn(Object JavaDoc arg0)
537     {
538         if (!_suppressWarnings)
539             message(WARN,arg0,new Frame(1));
540     }
541
542     /* (non-Javadoc)
543      * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
544      */

545     public void warn(Object JavaDoc arg0, Throwable JavaDoc arg1)
546     {
547         if (!_suppressWarnings)
548             message(WARN,new Object JavaDoc[]{arg0,arg1},new Frame(1));
549     }
550
551     /*-------------------------------------------------------------------*/
552     private boolean isDebugOnFor(Frame frame)
553     {
554         if (_debugOn)
555         {
556             if (_debugPatterns==null)
557                 return true;
558             else
559             {
560                 for (int i = _debugPatterns.size();--i>=0;)
561                 {
562                     if(frame.getWhere().indexOf((String JavaDoc)_debugPatterns
563                                                 .get(i))>=0)
564                         return true;
565                 }
566             }
567         }
568         return false;
569     }
570     
571 }
572
573
Popular Tags