KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > impl > ConsoleLogger


1 /*
2  * Copyright 1997-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.avalon.logging.impl;
17
18 import org.apache.avalon.framework.logger.Logger;
19
20 /**
21  * Logger sending everything to the standard output streams.
22  * This is mainly for the cases when you have a utility that
23  * does not have a logger to supply.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  * @version CVS $Revision: 1.1 $ $Date: 2004/03/01 13:39:28 $
27  */

28 public final class ConsoleLogger
29     implements Logger
30 {
31     /** Typecode for debugging messages. */
32     public static final int LEVEL_DEBUG = 0;
33
34     /** Typecode for informational messages. */
35     public static final int LEVEL_INFO = 1;
36
37     /** Typecode for warning messages. */
38     public static final int LEVEL_WARN = 2;
39
40     /** Typecode for error messages. */
41     public static final int LEVEL_ERROR = 3;
42
43     /** Typecode for fatal error messages. */
44     public static final int LEVEL_FATAL = 4;
45
46     /** Typecode for disabled log levels. */
47     public static final int LEVEL_DISABLED = 5;
48
49     private final int m_logLevel;
50
51     /**
52      * Creates a new ConsoleLogger with the priority set to DEBUG.
53      */

54     public ConsoleLogger()
55     {
56         this( LEVEL_DEBUG );
57     }
58
59     /**
60      * Creates a new ConsoleLogger.
61      * @param logLevel log level typecode
62      */

63     public ConsoleLogger( final int logLevel )
64     {
65         m_logLevel = logLevel;
66     }
67
68     /**
69      * Logs a debugging message.
70      *
71      * @param message a <code>String</code> value
72      */

73     public void debug( final String JavaDoc message )
74     {
75         debug( message, null );
76     }
77
78     /**
79      * Logs a debugging message and an exception.
80      *
81      * @param message a <code>String</code> value
82      * @param throwable a <code>Throwable</code> value
83      */

84     public void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
85     {
86         if( m_logLevel <= LEVEL_DEBUG )
87         {
88             System.out.print( "[DEBUG] " );
89             System.out.println( message );
90
91             if( null != throwable )
92             {
93                 throwable.printStackTrace( System.out );
94             }
95         }
96     }
97
98     /**
99      * Returns <code>true</code> if debug-level logging is enabled, false otherwise.
100      *
101      * @return <code>true</code> if debug-level logging
102      */

103     public boolean isDebugEnabled()
104     {
105         return m_logLevel <= LEVEL_DEBUG;
106     }
107
108     /**
109      * Logs an informational message.
110      *
111      * @param message a <code>String</code> value
112      */

113     public void info( final String JavaDoc message )
114     {
115         info( message, null );
116     }
117
118     /**
119      * Logs an informational message and an exception.
120      *
121      * @param message a <code>String</code> value
122      * @param throwable a <code>Throwable</code> value
123      */

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

143     public boolean isInfoEnabled()
144     {
145         return m_logLevel <= LEVEL_INFO;
146     }
147
148     /**
149      * Logs a warning message.
150      *
151      * @param message a <code>String</code> value
152      */

153     public void warn( final String JavaDoc message )
154     {
155         warn( message, null );
156     }
157
158     /**
159      * Logs a warning message and an exception.
160      *
161      * @param message a <code>String</code> value
162      * @param throwable a <code>Throwable</code> value
163      */

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

183     public boolean isWarnEnabled()
184     {
185         return m_logLevel <= LEVEL_WARN;
186     }
187
188     /**
189      * Logs an error message.
190      *
191      * @param message a <code>String</code> value
192      */

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

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

223     public boolean isErrorEnabled()
224     {
225         return m_logLevel <= LEVEL_ERROR;
226     }
227
228     /**
229      * Logs a fatal error message.
230      *
231      * @param message a <code>String</code> value
232      */

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

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

263     public boolean isFatalErrorEnabled()
264     {
265         return m_logLevel <= LEVEL_FATAL;
266     }
267
268     /**
269      * Just returns this logger (<code>ConsoleLogger</code> is not hierarchical).
270      *
271      * @param name ignored
272      * @return this logger
273      */

274     public Logger getChildLogger( final String JavaDoc name )
275     {
276         return this;
277     }
278 }
279
Popular Tags