1 16 17 18 package org.apache.commons.logging.impl; 19 20 import java.io.InputStream ; 21 import java.io.Serializable ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.text.DateFormat ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.Properties ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogConfigurationException; 33 34 75 public class SimpleLog implements Log, Serializable { 76 77 78 80 81 static protected final String systemPrefix = 82 "org.apache.commons.logging.simplelog."; 83 84 85 static protected final Properties simpleLogProps = new Properties (); 86 87 88 static protected final String DEFAULT_DATE_TIME_FORMAT = 89 "yyyy/MM/dd HH:mm:ss:SSS zzz"; 90 91 92 static protected boolean showLogName = false; 93 97 static protected boolean showShortName = true; 98 99 static protected boolean showDateTime = false; 100 101 static protected String dateTimeFormat = DEFAULT_DATE_TIME_FORMAT; 102 103 static protected DateFormat dateFormatter = null; 104 105 107 108 109 public static final int LOG_LEVEL_TRACE = 1; 110 111 public static final int LOG_LEVEL_DEBUG = 2; 112 113 public static final int LOG_LEVEL_INFO = 3; 114 115 public static final int LOG_LEVEL_WARN = 4; 116 117 public static final int LOG_LEVEL_ERROR = 5; 118 119 public static final int LOG_LEVEL_FATAL = 6; 120 121 122 public static final int LOG_LEVEL_ALL = (LOG_LEVEL_TRACE - 1); 123 124 125 public static final int LOG_LEVEL_OFF = (LOG_LEVEL_FATAL + 1); 126 127 129 private static String getStringProperty(String name) { 130 String prop = null; 131 try { 132 prop = System.getProperty(name); 133 } catch (SecurityException e) { 134 ; } 136 return (prop == null) ? simpleLogProps.getProperty(name) : prop; 137 } 138 139 private static String getStringProperty(String name, String dephault) { 140 String prop = getStringProperty(name); 141 return (prop == null) ? dephault : prop; 142 } 143 144 private static boolean getBooleanProperty(String name, boolean dephault) { 145 String prop = getStringProperty(name); 146 return (prop == null) ? dephault : "true".equalsIgnoreCase(prop); 147 } 148 149 static { 153 InputStream in = getResourceAsStream("simplelog.properties"); 155 if(null != in) { 156 try { 157 simpleLogProps.load(in); 158 in.close(); 159 } catch(java.io.IOException e) { 160 } 162 } 163 164 showLogName = getBooleanProperty( systemPrefix + "showlogname", showLogName); 165 showShortName = getBooleanProperty( systemPrefix + "showShortLogname", showShortName); 166 showDateTime = getBooleanProperty( systemPrefix + "showdatetime", showDateTime); 167 168 if(showDateTime) { 169 dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat", 170 dateTimeFormat); 171 try { 172 dateFormatter = new SimpleDateFormat (dateTimeFormat); 173 } catch(IllegalArgumentException e) { 174 dateTimeFormat = DEFAULT_DATE_TIME_FORMAT; 176 dateFormatter = new SimpleDateFormat (dateTimeFormat); 177 } 178 } 179 } 180 181 182 184 185 protected String logName = null; 186 187 protected int currentLogLevel; 188 189 private String shortLogName = null; 190 191 192 194 199 public SimpleLog(String name) { 200 201 logName = name; 202 203 setLevel(SimpleLog.LOG_LEVEL_INFO); 207 208 String lvl = getStringProperty(systemPrefix + "log." + logName); 210 int i = String.valueOf(name).lastIndexOf("."); 211 while(null == lvl && i > -1) { 212 name = name.substring(0,i); 213 lvl = getStringProperty(systemPrefix + "log." + name); 214 i = String.valueOf(name).lastIndexOf("."); 215 } 216 217 if(null == lvl) { 218 lvl = getStringProperty(systemPrefix + "defaultlog"); 219 } 220 221 if("all".equalsIgnoreCase(lvl)) { 222 setLevel(SimpleLog.LOG_LEVEL_ALL); 223 } else if("trace".equalsIgnoreCase(lvl)) { 224 setLevel(SimpleLog.LOG_LEVEL_TRACE); 225 } else if("debug".equalsIgnoreCase(lvl)) { 226 setLevel(SimpleLog.LOG_LEVEL_DEBUG); 227 } else if("info".equalsIgnoreCase(lvl)) { 228 setLevel(SimpleLog.LOG_LEVEL_INFO); 229 } else if("warn".equalsIgnoreCase(lvl)) { 230 setLevel(SimpleLog.LOG_LEVEL_WARN); 231 } else if("error".equalsIgnoreCase(lvl)) { 232 setLevel(SimpleLog.LOG_LEVEL_ERROR); 233 } else if("fatal".equalsIgnoreCase(lvl)) { 234 setLevel(SimpleLog.LOG_LEVEL_FATAL); 235 } else if("off".equalsIgnoreCase(lvl)) { 236 setLevel(SimpleLog.LOG_LEVEL_OFF); 237 } 238 239 } 240 241 242 244 249 public void setLevel(int currentLogLevel) { 250 251 this.currentLogLevel = currentLogLevel; 252 253 } 254 255 256 259 public int getLevel() { 260 261 return currentLogLevel; 262 } 263 264 265 267 268 277 protected void log(int type, Object message, Throwable t) { 278 StringBuffer buf = new StringBuffer (); 280 281 if(showDateTime) { 283 buf.append(dateFormatter.format(new Date ())); 284 buf.append(" "); 285 } 286 287 switch(type) { 289 case SimpleLog.LOG_LEVEL_TRACE: buf.append("[TRACE] "); break; 290 case SimpleLog.LOG_LEVEL_DEBUG: buf.append("[DEBUG] "); break; 291 case SimpleLog.LOG_LEVEL_INFO: buf.append("[INFO] "); break; 292 case SimpleLog.LOG_LEVEL_WARN: buf.append("[WARN] "); break; 293 case SimpleLog.LOG_LEVEL_ERROR: buf.append("[ERROR] "); break; 294 case SimpleLog.LOG_LEVEL_FATAL: buf.append("[FATAL] "); break; 295 } 296 297 if( showShortName) { 299 if( shortLogName==null ) { 300 shortLogName = logName.substring(logName.lastIndexOf(".") + 1); 302 shortLogName = 303 shortLogName.substring(shortLogName.lastIndexOf("/") + 1); 304 } 305 buf.append(String.valueOf(shortLogName)).append(" - "); 306 } else if(showLogName) { 307 buf.append(String.valueOf(logName)).append(" - "); 308 } 309 310 buf.append(String.valueOf(message)); 312 313 if(t != null) { 315 buf.append(" <"); 316 buf.append(t.toString()); 317 buf.append(">"); 318 319 java.io.StringWriter sw= new java.io.StringWriter (1024); 320 java.io.PrintWriter pw= new java.io.PrintWriter (sw); 321 t.printStackTrace(pw); 322 pw.close(); 323 buf.append(sw.toString()); 324 } 325 326 write(buf); 328 329 } 330 331 332 340 protected void write(StringBuffer buffer) { 341 342 System.err.println(buffer.toString()); 343 344 } 345 346 347 352 protected boolean isLevelEnabled(int logLevel) { 353 return (logLevel >= currentLogLevel); 356 } 357 358 359 361 362 365 public final void debug(Object message) { 366 367 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { 368 log(SimpleLog.LOG_LEVEL_DEBUG, message, null); 369 } 370 } 371 372 373 376 public final void debug(Object message, Throwable t) { 377 378 if (isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG)) { 379 log(SimpleLog.LOG_LEVEL_DEBUG, message, t); 380 } 381 } 382 383 384 387 public final void trace(Object message) { 388 389 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { 390 log(SimpleLog.LOG_LEVEL_TRACE, message, null); 391 } 392 } 393 394 395 398 public final void trace(Object message, Throwable t) { 399 400 if (isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE)) { 401 log(SimpleLog.LOG_LEVEL_TRACE, message, t); 402 } 403 } 404 405 406 409 public final void info(Object message) { 410 411 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { 412 log(SimpleLog.LOG_LEVEL_INFO,message,null); 413 } 414 } 415 416 417 420 public final void info(Object message, Throwable t) { 421 422 if (isLevelEnabled(SimpleLog.LOG_LEVEL_INFO)) { 423 log(SimpleLog.LOG_LEVEL_INFO, message, t); 424 } 425 } 426 427 428 431 public final void warn(Object message) { 432 433 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { 434 log(SimpleLog.LOG_LEVEL_WARN, message, null); 435 } 436 } 437 438 439 442 public final void warn(Object message, Throwable t) { 443 444 if (isLevelEnabled(SimpleLog.LOG_LEVEL_WARN)) { 445 log(SimpleLog.LOG_LEVEL_WARN, message, t); 446 } 447 } 448 449 450 453 public final void error(Object message) { 454 455 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { 456 log(SimpleLog.LOG_LEVEL_ERROR, message, null); 457 } 458 } 459 460 461 464 public final void error(Object message, Throwable t) { 465 466 if (isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR)) { 467 log(SimpleLog.LOG_LEVEL_ERROR, message, t); 468 } 469 } 470 471 472 475 public final void fatal(Object message) { 476 477 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { 478 log(SimpleLog.LOG_LEVEL_FATAL, message, null); 479 } 480 } 481 482 483 486 public final void fatal(Object message, Throwable t) { 487 488 if (isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL)) { 489 log(SimpleLog.LOG_LEVEL_FATAL, message, t); 490 } 491 } 492 493 494 501 public final boolean isDebugEnabled() { 502 503 return isLevelEnabled(SimpleLog.LOG_LEVEL_DEBUG); 504 } 505 506 507 514 public final boolean isErrorEnabled() { 515 516 return isLevelEnabled(SimpleLog.LOG_LEVEL_ERROR); 517 } 518 519 520 527 public final boolean isFatalEnabled() { 528 529 return isLevelEnabled(SimpleLog.LOG_LEVEL_FATAL); 530 } 531 532 533 540 public final boolean isInfoEnabled() { 541 542 return isLevelEnabled(SimpleLog.LOG_LEVEL_INFO); 543 } 544 545 546 553 public final boolean isTraceEnabled() { 554 555 return isLevelEnabled(SimpleLog.LOG_LEVEL_TRACE); 556 } 557 558 559 566 public final boolean isWarnEnabled() { 567 568 return isLevelEnabled(SimpleLog.LOG_LEVEL_WARN); 569 } 570 571 572 582 private static ClassLoader getContextClassLoader() 583 { 584 ClassLoader classLoader = null; 585 586 if (classLoader == null) { 587 try { 588 Method method = Thread .class.getMethod("getContextClassLoader", null); 590 591 try { 593 classLoader = (ClassLoader )method.invoke(Thread.currentThread(), null); 594 } catch (IllegalAccessException e) { 595 ; } catch (InvocationTargetException e) { 597 613 if (e.getTargetException() instanceof SecurityException ) { 614 ; } else { 616 throw new LogConfigurationException 619 ("Unexpected InvocationTargetException", e.getTargetException()); 620 } 621 } 622 } catch (NoSuchMethodException e) { 623 ; } 626 } 627 628 if (classLoader == null) { 629 classLoader = SimpleLog.class.getClassLoader(); 630 } 631 632 return classLoader; 634 } 635 636 private static InputStream getResourceAsStream(final String name) 637 { 638 return (InputStream )AccessController.doPrivileged( 639 new PrivilegedAction () { 640 public Object run() { 641 ClassLoader threadCL = getContextClassLoader(); 642 643 if (threadCL != null) { 644 return threadCL.getResourceAsStream(name); 645 } else { 646 return ClassLoader.getSystemResourceAsStream(name); 647 } 648 } 649 }); 650 } 651 } 652 653 | Popular Tags |