1 16 17 package org.apache.log4j; 18 19 import org.apache.log4j.Layout; 20 import org.apache.log4j.spi.LoggingEvent; 21 import org.apache.log4j.helpers.PatternParser; 22 import org.apache.log4j.helpers.PatternConverter; 23 24 25 28 394 public class PatternLayout extends Layout { 395 396 397 400 public final static String DEFAULT_CONVERSION_PATTERN ="%m%n"; 401 402 404 public final static String TTCC_CONVERSION_PATTERN 405 = "%r [%t] %p %c %x - %m%n"; 406 407 408 protected final int BUF_SIZE = 256; 409 protected final int MAX_CAPACITY = 1024; 410 411 412 private StringBuffer sbuf = new StringBuffer (BUF_SIZE); 414 415 private String pattern; 416 417 private PatternConverter head; 418 419 private String timezone; 420 421 426 public PatternLayout() { 427 this(DEFAULT_CONVERSION_PATTERN); 428 } 429 430 433 public PatternLayout(String pattern) { 434 this.pattern = pattern; 435 head = createPatternParser((pattern == null) ? DEFAULT_CONVERSION_PATTERN : 436 pattern).parse(); 437 } 438 439 444 public 445 void setConversionPattern(String conversionPattern) { 446 pattern = conversionPattern; 447 head = createPatternParser(conversionPattern).parse(); 448 } 449 450 453 public 454 String getConversionPattern() { 455 return pattern; 456 } 457 458 461 public 462 void activateOptions() { 463 } 465 466 472 public 473 boolean ignoresThrowable() { 474 return true; 475 } 476 477 484 protected PatternParser createPatternParser(String pattern) { 485 return new PatternParser(pattern); 486 } 487 488 489 492 public String format(LoggingEvent event) { 493 if(sbuf.capacity() > MAX_CAPACITY) { 495 sbuf = new StringBuffer (BUF_SIZE); 496 } else { 497 sbuf.setLength(0); 498 } 499 500 PatternConverter c = head; 501 502 while(c != null) { 503 c.format(sbuf, event); 504 c = c.next; 505 } 506 return sbuf.toString(); 507 } 508 } 509 | Popular Tags |