1 8 package org.codehaus.dna.impl; 9 10 import org.codehaus.dna.Logger; 11 12 import java.io.PrintStream ; 13 14 19 public class ConsoleLogger 20 implements Logger 21 { 22 26 public static final int LEVEL_ALL = 0; 27 28 32 public static final int LEVEL_TRACE = 1; 33 34 38 public static final int LEVEL_DEBUG = 2; 39 40 44 public static final int LEVEL_INFO = 3; 45 46 50 public static final int LEVEL_WARN = 4; 51 52 56 public static final int LEVEL_ERROR = 5; 57 58 62 public static final int LEVEL_NONE = 6; 63 64 67 private static final String LEVEL_TRACE_STR = "TRACE"; 68 69 72 private static final String LEVEL_DEBUG_STR = "DEBUG"; 73 74 77 private static final String LEVEL_INFO_STR = "INFO"; 78 79 82 private static final String LEVEL_WARN_STR = "WARN"; 83 84 87 private static final String LEVEL_ERROR_STR = "ERROR"; 88 89 92 private final int m_level; 93 94 97 private final PrintStream m_output; 98 99 102 public ConsoleLogger() 103 { 104 this( LEVEL_ALL ); 105 } 106 107 112 public ConsoleLogger( final int level ) 113 { 114 this( level, System.out ); 115 } 116 117 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 139 public void trace( final String message ) 140 { 141 trace( message, null ); 142 } 143 144 150 public void trace( final String message, 151 final Throwable throwable ) 152 { 153 output( LEVEL_TRACE, LEVEL_TRACE_STR, message, throwable ); 154 } 155 156 161 public boolean isTraceEnabled() 162 { 163 return m_level <= LEVEL_TRACE; 164 } 165 166 171 public void debug( final String message ) 172 { 173 debug( message, null ); 174 } 175 176 182 public void debug( final String message, 183 final Throwable throwable ) 184 { 185 output( LEVEL_DEBUG, LEVEL_DEBUG_STR, message, throwable ); 186 } 187 188 193 public boolean isDebugEnabled() 194 { 195 return m_level <= LEVEL_DEBUG; 196 } 197 198 203 public void info( final String message ) 204 { 205 info( message, null ); 206 } 207 208 214 public void info( final String message, 215 final Throwable throwable ) 216 { 217 output( LEVEL_INFO, LEVEL_INFO_STR, message, throwable ); 218 } 219 220 225 public boolean isInfoEnabled() 226 { 227 return m_level <= LEVEL_INFO; 228 } 229 230 235 public void warn( final String message ) 236 { 237 warn( message, null ); 238 } 239 240 246 public void warn( final String message, 247 final Throwable throwable ) 248 { 249 output( LEVEL_WARN, LEVEL_WARN_STR, message, throwable ); 250 } 251 252 257 public boolean isWarnEnabled() 258 { 259 return m_level <= LEVEL_WARN; 260 } 261 262 267 public void error( final String message ) 268 { 269 error( message, null ); 270 } 271 272 278 public void error( final String message, 279 final Throwable throwable ) 280 { 281 output( LEVEL_ERROR, LEVEL_ERROR_STR, message, throwable ); 282 } 283 284 289 public boolean isErrorEnabled() 290 { 291 return m_level <= LEVEL_ERROR; 292 } 293 294 300 public Logger getChildLogger( final String name ) 301 { 302 return this; 303 } 304 305 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 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 351 final int getLevel() 352 { 353 return m_level; 354 } 355 } 356 | Popular Tags |