1 61 62 63 package org.apache.commons.discovery.log; 64 65 import java.io.PrintStream ; 66 import java.text.DateFormat ; 67 import java.text.SimpleDateFormat ; 68 import java.util.Date ; 69 70 import org.apache.commons.logging.Log; 71 72 73 94 public class SimpleLog implements Log { 95 97 98 public static final int LOG_LEVEL_TRACE = 1; 99 100 public static final int LOG_LEVEL_DEBUG = 2; 101 102 public static final int LOG_LEVEL_INFO = 3; 103 104 public static final int LOG_LEVEL_WARN = 4; 105 106 public static final int LOG_LEVEL_ERROR = 5; 107 108 public static final int LOG_LEVEL_FATAL = 6; 109 110 111 public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1); 112 113 114 public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1); 115 116 118 static protected final String PROP_LEVEL = 119 "org.apache.commons.discovery.log.level"; 120 121 122 static protected boolean showLogName = false; 123 124 128 static protected boolean showShortName = true; 129 130 131 static protected boolean showDateTime = false; 132 133 134 static protected DateFormat dateFormatter = null; 135 136 137 static protected int logLevel = LOG_LEVEL_INFO; 138 139 140 144 static private PrintStream out = System.out; 145 146 148 static { 150 if(showDateTime) { 151 dateFormatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss:SSS zzz"); 152 } 153 154 String lvl = System.getProperty(PROP_LEVEL); 156 157 if("all".equalsIgnoreCase(lvl)) { 158 setLevel(SimpleLog.LOG_LEVEL_ALL); 159 } else if("trace".equalsIgnoreCase(lvl)) { 160 setLevel(SimpleLog.LOG_LEVEL_TRACE); 161 } else if("debug".equalsIgnoreCase(lvl)) { 162 setLevel(SimpleLog.LOG_LEVEL_DEBUG); 163 } else if("info".equalsIgnoreCase(lvl)) { 164 setLevel(SimpleLog.LOG_LEVEL_INFO); 165 } else if("warn".equalsIgnoreCase(lvl)) { 166 setLevel(SimpleLog.LOG_LEVEL_WARN); 167 } else if("error".equalsIgnoreCase(lvl)) { 168 setLevel(SimpleLog.LOG_LEVEL_ERROR); 169 } else if("fatal".equalsIgnoreCase(lvl)) { 170 setLevel(SimpleLog.LOG_LEVEL_FATAL); 171 } else if("off".equalsIgnoreCase(lvl)) { 172 setLevel(SimpleLog.LOG_LEVEL_OFF); 173 } 174 } 175 176 178 183 public static void setLevel(int currentLogLevel) { 184 logLevel = currentLogLevel; 185 } 186 187 190 public static int getLevel() { 191 return logLevel; 192 } 193 194 199 protected static boolean isLevelEnabled(int level) { 200 return (level >= getLevel()); 203 } 204 205 206 207 209 210 protected String logName = null; 211 212 private String prefix=null; 213 214 215 217 222 public SimpleLog(String name) { 223 logName = name; 224 } 225 226 227 229 230 235 protected void log(int type, Object message, Throwable t) { 236 StringBuffer buf = new StringBuffer (); 238 239 if(showDateTime) { 241 buf.append(dateFormatter.format(new Date ())); 242 buf.append(" "); 243 } 244 245 switch(type) { 247 case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break; 248 case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break; 249 case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO ] "); break; 250 case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN ] "); break; 251 case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break; 252 case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break; 253 } 254 255 if( showShortName) { 257 if( prefix==null ) { 258 prefix = logName.substring( logName.lastIndexOf(".") +1) + " - "; 260 prefix = prefix.substring( prefix.lastIndexOf("/") +1) + "-"; 261 } 262 buf.append( prefix ); 263 } else if(showLogName) { 264 buf.append(String.valueOf(logName)).append(" - "); 265 } 266 267 buf.append(String.valueOf(message)); 269 270 if(t != null) { 272 buf.append(" <"); 273 buf.append(t.toString()); 274 buf.append(">"); 275 } 276 277 out.println(buf.toString()); 279 280 if (t != null) 281 t.printStackTrace(System.err); 282 } 283 284 285 287 288 291 public final void debug(Object message) { 292 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { 293 log(SimpleLog.LOG_LEVEL_DEBUG, message, null); 294 } 295 } 296 297 298 301 public final void debug(Object message, Throwable t) { 302 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { 303 log(SimpleLog.LOG_LEVEL_DEBUG, message, t); 304 } 305 } 306 307 308 311 public final void trace(Object message) { 312 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { 313 log(SimpleLog.LOG_LEVEL_TRACE, message, null); 314 } 315 } 316 317 318 321 public final void trace(Object message, Throwable t) { 322 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { 323 log(SimpleLog.LOG_LEVEL_TRACE, message, t); 324 } 325 } 326 327 328 331 public final void info(Object message) { 332 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { 333 log(SimpleLog.LOG_LEVEL_INFO,message,null); 334 } 335 } 336 337 338 341 public final void info(Object message, Throwable t) { 342 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { 343 log(SimpleLog.LOG_LEVEL_INFO, message, t); 344 } 345 } 346 347 348 351 public final void warn(Object message) { 352 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { 353 log(SimpleLog.LOG_LEVEL_WARN, message, null); 354 } 355 } 356 357 358 361 public final void warn(Object message, Throwable t) { 362 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { 363 log(SimpleLog.LOG_LEVEL_WARN, message, t); 364 } 365 } 366 367 368 371 public final void error(Object message) { 372 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { 373 log(SimpleLog.LOG_LEVEL_ERROR, message, null); 374 } 375 } 376 377 378 381 public final void error(Object message, Throwable t) { 382 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { 383 log(SimpleLog.LOG_LEVEL_ERROR, message, t); 384 } 385 } 386 387 388 391 public final void fatal(Object message) { 392 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { 393 log(SimpleLog.LOG_LEVEL_FATAL, message, null); 394 } 395 } 396 397 398 401 public final void fatal(Object message, Throwable t) { 402 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { 403 log(SimpleLog.LOG_LEVEL_FATAL, message, t); 404 } 405 } 406 407 408 415 public final boolean isDebugEnabled() { 416 return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG); 417 } 418 419 420 427 public final boolean isErrorEnabled() { 428 return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR); 429 } 430 431 432 439 public final boolean isFatalEnabled() { 440 return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL); 441 } 442 443 444 451 public final boolean isInfoEnabled() { 452 return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO); 453 } 454 455 456 463 public final boolean isTraceEnabled() { 464 return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE); 465 } 466 467 468 475 public final boolean isWarnEnabled() { 476 return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN); 477 } 478 } 479 | Popular Tags |