01: package de.webman.util.log4j;
02:
03: import org.apache.log4j.*;
04: import org.apache.log4j.helpers.PatternParser;
05: import org.apache.log4j.spi.LoggingEvent;
06:
07: /**
08: *
09: * Adds functionality to handle the stacktrace of throwables
10: * @see org.apache.log4j.PatternLayout
11: * @author alexander grosse
12: * @version $Revision: 1.1 $
13: */
14: public class WebmanPatternLayout extends PatternLayout {
15: final static int DEFAULT_BUFFER_SIZE = 128;
16: StringBuffer sbuf = new StringBuffer(DEFAULT_BUFFER_SIZE);
17:
18: public WebmanPatternLayout() {
19: this (DEFAULT_CONVERSION_PATTERN);
20: }
21:
22: public WebmanPatternLayout(String pattern) {
23: super (pattern);
24: }
25:
26: public PatternParser createPatternParser(String pattern) {
27: return new WebmanPatternParser(
28: pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern);
29: }
30:
31: public String format(LoggingEvent event) {
32:
33: sbuf.setLength(0);
34: sbuf.append(event.priority.toString());
35: sbuf.append(" - ");
36: sbuf.append(event.getMessage());
37: sbuf.append(LINE_SEP);
38: return sbuf.toString();
39: }
40:
41: /**
42: we do handle the throwable
43: */
44: public boolean ignoresThrowable() {
45: return false;
46: }
47:
48: public static void main(String[] args) {
49: Layout layout = new WebmanPatternLayout("[exception=%#] - %m%n");
50: Category cat = Category.getInstance("some.cat");
51: cat.addAppender(new ConsoleAppender(layout));
52: Exception e = new Exception("text");
53: //e.printStackTrace();
54: cat.debug("Hello, log", e);
55: cat.info("Hello again...", e);
56: }
57: }
|