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