|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConsoleLogger.java | 100% | 100% | 100% | 100% |
|
1 |
/*
|
|
2 |
* Copyright (C) The DNA Group. All rights reserved.
|
|
3 |
*
|
|
4 |
* This software is published under the terms of the DNA
|
|
5 |
* Software License version 1.1, a copy of which has been included
|
|
6 |
* with this distribution in the LICENSE.txt file.
|
|
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 | 12 |
public ConsoleLogger()
|
103 |
{ |
|
104 | 12 |
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 | 120 |
public ConsoleLogger( final int level ) |
113 |
{ |
|
114 | 120 |
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 | 132 |
public ConsoleLogger( final int level, |
124 |
final PrintStream output ) |
|
125 |
{ |
|
126 | 132 |
if( null == output ) |
127 |
{ |
|
128 | 4 |
throw new NullPointerException( "output" ); |
129 |
} |
|
130 | 128 |
m_level = level; |
131 | 128 |
m_output = output; |
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Log a trace message.
|
|
136 |
*
|
|
137 |
* @param message the message
|
|
138 |
*/
|
|
139 | 8 |
public void trace( final String message ) |
140 |
{ |
|
141 | 8 |
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 | 16 |
public void trace( final String message, |
151 |
final Throwable throwable ) |
|
152 |
{ |
|
153 | 16 |
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 | 28 |
public boolean isTraceEnabled() |
162 |
{ |
|
163 | 28 |
return m_level <= LEVEL_TRACE;
|
164 |
} |
|
165 |
|
|
166 |
/**
|
|
167 |
* Log a debug message.
|
|
168 |
*
|
|
169 |
* @param message the message
|
|
170 |
*/
|
|
171 | 8 |
public void debug( final String message ) |
172 |
{ |
|
173 | 8 |
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 | 24 |
public void debug( final String message, |
183 |
final Throwable throwable ) |
|
184 |
{ |
|
185 | 24 |
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 | 28 |
public boolean isDebugEnabled() |
194 |
{ |
|
195 | 28 |
return m_level <= LEVEL_DEBUG;
|
196 |
} |
|
197 |
|
|
198 |
/**
|
|
199 |
* Log a info message.
|
|
200 |
*
|
|
201 |
* @param message the message
|
|
202 |
*/
|
|
203 | 8 |
public void info( final String message ) |
204 |
{ |
|
205 | 8 |
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 | 16 |
public void info( final String message, |
215 |
final Throwable throwable ) |
|
216 |
{ |
|
217 | 16 |
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 | 28 |
public boolean isInfoEnabled() |
226 |
{ |
|
227 | 28 |
return m_level <= LEVEL_INFO;
|
228 |
} |
|
229 |
|
|
230 |
/**
|
|
231 |
* Log a warn message.
|
|
232 |
*
|
|
233 |
* @param message the message
|
|
234 |
*/
|
|
235 | 8 |
public void warn( final String message ) |
236 |
{ |
|
237 | 8 |
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 | 16 |
public void warn( final String message, |
247 |
final Throwable throwable ) |
|
248 |
{ |
|
249 | 16 |
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 | 28 |
public boolean isWarnEnabled() |
258 |
{ |
|
259 | 28 |
return m_level <= LEVEL_WARN;
|
260 |
} |
|
261 |
|
|
262 |
/**
|
|
263 |
* Log a error message.
|
|
264 |
*
|
|
265 |
* @param message the message
|
|
266 |
*/
|
|
267 | 8 |
public void error( final String message ) |
268 |
{ |
|
269 | 8 |
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 | 16 |
public void error( final String message, |
279 |
final Throwable throwable ) |
|
280 |
{ |
|
281 | 16 |
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 | 28 |
public boolean isErrorEnabled() |
290 |
{ |
|
291 | 28 |
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 | 4 |
public Logger getChildLogger( final String name )
|
301 |
{ |
|
302 | 4 |
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 | 88 |
private void output( final int level, |
315 |
final String type, |
|
316 |
final String message, |
|
317 |
final Throwable throwable ) |
|
318 |
{ |
|
319 | 88 |
if( m_level <= level )
|
320 |
{ |
|
321 | 48 |
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 | 8 |
void doOutput( final String type,
|
333 |
final String message, |
|
334 |
final Throwable throwable ) |
|
335 |
{ |
|
336 | 8 |
synchronized( System.out )
|
337 |
{ |
|
338 | 8 |
m_output.println( "[" + type + "] " + message ); |
339 | 8 |
if( null != throwable ) |
340 |
{ |
|
341 | 4 |
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 | 4 |
final int getLevel()
|
352 |
{ |
|
353 | 4 |
return m_level;
|
354 |
} |
|
355 |
} |
|
356 |
|
|