1
2
3
4
5
6
7
8 package org.codehaus.dna.impl;
9
10 import org.codehaus.dna.Logger;
11
12 import java.io.PrintStream;
13
14 /***
15 * A simple logger facade that simply writes to the Console.
16 *
17 * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
18 */
19 public class ConsoleLogger
20 implements Logger
21 {
22 /***
23 * Constant to indicate that the logger
24 * must log all levels.
25 */
26 public static final int LEVEL_ALL = 0;
27
28 /***
29 * Constant to indicate that the logger
30 * must log all levels TRACE and above.
31 */
32 public static final int LEVEL_TRACE = 1;
33
34 /***
35 * Constant to indicate that the logger
36 * must log all levels DEBUG and above.
37 */
38 public static final int LEVEL_DEBUG = 2;
39
40 /***
41 * Constant to indicate that the logger
42 * must log all levels INFO and above.
43 */
44 public static final int LEVEL_INFO = 3;
45
46 /***
47 * Constant to indicate that the logger
48 * must log all levels WARN and above.
49 */
50 public static final int LEVEL_WARN = 4;
51
52 /***
53 * Constant to indicate that the logger
54 * must log all levels ERROR and above.
55 */
56 public static final int LEVEL_ERROR = 5;
57
58 /***
59 * Constant to indicate that the logger
60 * must not log any messages.
61 */
62 public static final int LEVEL_NONE = 6;
63
64 /***
65 * String constant used to output TRACE messages.
66 */
67 private static final String LEVEL_TRACE_STR = "TRACE";
68
69 /***
70 * String constant used to output DEBUG messages.
71 */
72 private static final String LEVEL_DEBUG_STR = "DEBUG";
73
74 /***
75 * String constant used to output INFO messages.
76 */
77 private static final String LEVEL_INFO_STR = "INFO";
78
79 /***
80 * String constant used to output WARN messages.
81 */
82 private static final String LEVEL_WARN_STR = "WARN";
83
84 /***
85 * String constant used to output ERROR messages.
86 */
87 private static final String LEVEL_ERROR_STR = "ERROR";
88
89 /***
90 * The log level.
91 */
92 private final int m_level;
93
94 /***
95 * The output location.
96 */
97 private final PrintStream m_output;
98
99 /***
100 * Create a Console Logger that logs all messages.
101 */
102 public ConsoleLogger()
103 {
104 this( LEVEL_ALL );
105 }
106
107 /***
108 * Create a Console Logger that logs at specified level.
109 *
110 * @param level one of the LEVEL_* constants
111 */
112 public ConsoleLogger( final int level )
113 {
114 this( level, System.out );
115 }
116
117 /***
118 * Create a Console Logger that logs at specified level.
119 *
120 * @param level one of the LEVEL_* constants
121 * @param output the stream to output to
122 */
123 public ConsoleLogger( final int level,
124 final PrintStream output )
125 {
126 if( null == output )
127 {
128 throw new NullPointerException( "output" );
129 }
130 m_level = level;
131 m_output = output;
132 }
133
134 /***
135 * Log a trace message.
136 *
137 * @param message the message
138 */
139 public void trace( final String message )
140 {
141 trace( message, null );
142 }
143
144 /***
145 * Log a trace message with an associated throwable.
146 *
147 * @param message the message
148 * @param throwable the throwable
149 */
150 public void trace( final String message,
151 final Throwable throwable )
152 {
153 output( LEVEL_TRACE, LEVEL_TRACE_STR, message, throwable );
154 }
155
156 /***
157 * Return true if a trace message will be logged.
158 *
159 * @return true if message will be logged
160 */
161 public boolean isTraceEnabled()
162 {
163 return m_level <= LEVEL_TRACE;
164 }
165
166 /***
167 * Log a debug message.
168 *
169 * @param message the message
170 */
171 public void debug( final String message )
172 {
173 debug( message, null );
174 }
175
176 /***
177 * Log a debug message with an associated throwable.
178 *
179 * @param message the message
180 * @param throwable the throwable
181 */
182 public void debug( final String message,
183 final Throwable throwable )
184 {
185 output( LEVEL_DEBUG, LEVEL_DEBUG_STR, message, throwable );
186 }
187
188 /***
189 * Return true if a debug message will be logged.
190 *
191 * @return true if message will be logged
192 */
193 public boolean isDebugEnabled()
194 {
195 return m_level <= LEVEL_DEBUG;
196 }
197
198 /***
199 * Log a info message.
200 *
201 * @param message the message
202 */
203 public void info( final String message )
204 {
205 info( message, null );
206 }
207
208 /***
209 * Log a info message with an associated throwable.
210 *
211 * @param message the message
212 * @param throwable the throwable
213 */
214 public void info( final String message,
215 final Throwable throwable )
216 {
217 output( LEVEL_INFO, LEVEL_INFO_STR, message, throwable );
218 }
219
220 /***
221 * Return true if an info message will be logged.
222 *
223 * @return true if message will be logged
224 */
225 public boolean isInfoEnabled()
226 {
227 return m_level <= LEVEL_INFO;
228 }
229
230 /***
231 * Log a warn message.
232 *
233 * @param message the message
234 */
235 public void warn( final String message )
236 {
237 warn( message, null );
238 }
239
240 /***
241 * Log a warn message with an associated throwable.
242 *
243 * @param message the message
244 * @param throwable the throwable
245 */
246 public void warn( final String message,
247 final Throwable throwable )
248 {
249 output( LEVEL_WARN, LEVEL_WARN_STR, message, throwable );
250 }
251
252 /***
253 * Return true if a warn message will be logged.
254 *
255 * @return true if message will be logged
256 */
257 public boolean isWarnEnabled()
258 {
259 return m_level <= LEVEL_WARN;
260 }
261
262 /***
263 * Log a error message.
264 *
265 * @param message the message
266 */
267 public void error( final String message )
268 {
269 error( message, null );
270 }
271
272 /***
273 * Log a error message with an associated throwable.
274 *
275 * @param message the message
276 * @param throwable the throwable
277 */
278 public void error( final String message,
279 final Throwable throwable )
280 {
281 output( LEVEL_ERROR, LEVEL_ERROR_STR, message, throwable );
282 }
283
284 /***
285 * Return true if a error message will be logged.
286 *
287 * @return true if message will be logged
288 */
289 public boolean isErrorEnabled()
290 {
291 return m_level <= LEVEL_ERROR;
292 }
293
294 /***
295 * Get the child logger with specified name.
296 *
297 * @param name the name of child logger
298 * @return the child logger
299 */
300 public Logger getChildLogger( final String name )
301 {
302 return this;
303 }
304
305 /***
306 * Utility method that logs output if level
307 * is enabled.
308 *
309 * @param level the log level
310 * @param type the type string
311 * @param message the message
312 * @param throwable the throwable, may be null
313 */
314 private void output( final int level,
315 final String type,
316 final String message,
317 final Throwable throwable )
318 {
319 if( m_level <= level )
320 {
321 doOutput( type, message, throwable );
322 }
323 }
324
325 /***
326 * Utility method to actually output message to console.
327 *
328 * @param type the type string
329 * @param message the message
330 * @param throwable the throwable, may be null
331 */
332 void doOutput( final String type,
333 final String message,
334 final Throwable throwable )
335 {
336 synchronized( System.out )
337 {
338 m_output.println( "[" + type + "] " + message );
339 if( null != throwable )
340 {
341 throwable.printStackTrace( m_output );
342 }
343 }
344 }
345
346 /***
347 * Utility method so that subclasses can access log level.
348 *
349 * @return log level of logger
350 */
351 final int getLevel()
352 {
353 return m_level;
354 }
355 }