KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > logger > ConsoleLogger


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

55 package org.apache.avalon.framework.logger;
56
57 /**
58  * Logger sending everything to the standard output streams.
59  * This is mainly for the cases when you have a utility that
60  * does not have a logger to supply.
61  *
62  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
63  * @version CVS $Revision: 1.12 $ $Date: 2003/02/11 15:58:40 $
64  */

65 public final class ConsoleLogger
66     implements Logger
67 {
68     /** Typecode for debugging messages. */
69     public static final int LEVEL_DEBUG = 0;
70
71     /** Typecode for informational messages. */
72     public static final int LEVEL_INFO = 1;
73
74     /** Typecode for warning messages. */
75     public static final int LEVEL_WARN = 2;
76
77     /** Typecode for error messages. */
78     public static final int LEVEL_ERROR = 3;
79
80     /** Typecode for fatal error messages. */
81     public static final int LEVEL_FATAL = 4;
82
83     /** Typecode for disabled log levels. */
84     public static final int LEVEL_DISABLED = 5;
85
86     private final int m_logLevel;
87
88     /**
89      * Creates a new ConsoleLogger with the priority set to DEBUG.
90      */

91     public ConsoleLogger()
92     {
93         this( LEVEL_DEBUG );
94     }
95
96     /**
97      * Creates a new ConsoleLogger.
98      * @param logLevel log level typecode
99      */

100     public ConsoleLogger( final int logLevel )
101     {
102         m_logLevel = logLevel;
103     }
104
105     /**
106      * Logs a debugging message.
107      *
108      * @param message a <code>String</code> value
109      */

110     public void debug( final String JavaDoc message )
111     {
112         debug( message, null );
113     }
114
115     /**
116      * Logs a debugging message and an exception.
117      *
118      * @param message a <code>String</code> value
119      * @param throwable a <code>Throwable</code> value
120      */

121     public void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
122     {
123         if( m_logLevel <= LEVEL_DEBUG )
124         {
125             System.out.print( "[DEBUG] " );
126             System.out.println( message );
127
128             if( null != throwable )
129             {
130                 throwable.printStackTrace( System.out );
131             }
132         }
133     }
134
135     /**
136      * Returns <code>true</code> if debug-level logging is enabled, false otherwise.
137      *
138      * @return <code>true</code> if debug-level logging
139      */

140     public boolean isDebugEnabled()
141     {
142         return m_logLevel <= LEVEL_DEBUG;
143     }
144
145     /**
146      * Logs an informational message.
147      *
148      * @param message a <code>String</code> value
149      */

150     public void info( final String JavaDoc message )
151     {
152         info( message, null );
153     }
154
155     /**
156      * Logs an informational message and an exception.
157      *
158      * @param message a <code>String</code> value
159      * @param throwable a <code>Throwable</code> value
160      */

161     public void info( final String JavaDoc message, final Throwable JavaDoc throwable )
162     {
163         if( m_logLevel <= LEVEL_INFO )
164         {
165             System.out.print( "[INFO] " );
166             System.out.println( message );
167
168             if( null != throwable )
169             {
170                 throwable.printStackTrace( System.out );
171             }
172         }
173     }
174
175     /**
176      * Returns <code>true</code> if info-level logging is enabled, false otherwise.
177      *
178      * @return <code>true</code> if info-level logging is enabled
179      */

180     public boolean isInfoEnabled()
181     {
182         return m_logLevel <= LEVEL_INFO;
183     }
184
185     /**
186      * Logs a warning message.
187      *
188      * @param message a <code>String</code> value
189      */

190     public void warn( final String JavaDoc message )
191     {
192         warn( message, null );
193     }
194
195     /**
196      * Logs a warning message and an exception.
197      *
198      * @param message a <code>String</code> value
199      * @param throwable a <code>Throwable</code> value
200      */

201     public void warn( final String JavaDoc message, final Throwable JavaDoc throwable )
202     {
203         if( m_logLevel <= LEVEL_WARN )
204         {
205             System.out.print( "[WARNING] " );
206             System.out.println( message );
207
208             if( null != throwable )
209             {
210                 throwable.printStackTrace( System.out );
211             }
212         }
213     }
214
215     /**
216      * Returns <code>true</code> if warn-level logging is enabled, false otherwise.
217      *
218      * @return <code>true</code> if warn-level logging is enabled
219      */

220     public boolean isWarnEnabled()
221     {
222         return m_logLevel <= LEVEL_WARN;
223     }
224
225     /**
226      * Logs an error message.
227      *
228      * @param message a <code>String</code> value
229      */

230     public void error( final String JavaDoc message )
231     {
232         error( message, null );
233     }
234
235     /**
236      * Logs an error message and an exception.
237      *
238      * @param message a <code>String</code> value
239      * @param throwable a <code>Throwable</code> value
240      */

241     public void error( final String JavaDoc message, final Throwable JavaDoc throwable )
242     {
243         if( m_logLevel <= LEVEL_ERROR )
244         {
245             System.out.print( "[ERROR] " );
246             System.out.println( message );
247
248             if( null != throwable )
249             {
250                 throwable.printStackTrace( System.out );
251             }
252         }
253     }
254
255     /**
256      * Returns <code>true</code> if error-level logging is enabled, false otherwise.
257      *
258      * @return <code>true</code> if error-level logging is enabled
259      */

260     public boolean isErrorEnabled()
261     {
262         return m_logLevel <= LEVEL_ERROR;
263     }
264
265     /**
266      * Logs a fatal error message.
267      *
268      * @param message a <code>String</code> value
269      */

270     public void fatalError( final String JavaDoc message )
271     {
272         fatalError( message, null );
273     }
274
275     /**
276      * Logs a fatal error message and an exception.
277      *
278      * @param message a <code>String</code> value
279      * @param throwable a <code>Throwable</code> value
280      */

281     public void fatalError( final String JavaDoc message, final Throwable JavaDoc throwable )
282     {
283         if( m_logLevel <= LEVEL_FATAL )
284         {
285             System.out.print( "[FATAL ERROR] " );
286             System.out.println( message );
287
288             if( null != throwable )
289             {
290                 throwable.printStackTrace( System.out );
291             }
292         }
293     }
294
295     /**
296      * Returns <code>true</code> if fatal-level logging is enabled, false otherwise.
297      *
298      * @return <code>true</code> if fatal-level logging is enabled
299      */

300     public boolean isFatalErrorEnabled()
301     {
302         return m_logLevel <= LEVEL_FATAL;
303     }
304
305     /**
306      * Just returns this logger (<code>ConsoleLogger</code> is not hierarchical).
307      *
308      * @param name ignored
309      * @return this logger
310      */

311     public Logger getChildLogger( final String JavaDoc name )
312     {
313         return this;
314     }
315 }
316
Popular Tags