1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.spi.ThrowableInformation;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class ThrowableInformationPatternConverter
36 extends LoggingEventPatternConverter {
37
38
39
40
41 private int maxLines = Integer.MAX_VALUE;
42
43
44
45
46
47 private ThrowableInformationPatternConverter(
48 final String[] options) {
49 super("Throwable", "throwable");
50
51 if ((options != null) && (options.length > 0)) {
52 if("none".equals(options[0])) {
53 maxLines = 0;
54 } else if("short".equals(options[0])) {
55 maxLines = 1;
56 } else {
57 try {
58 maxLines = Integer.parseInt(options[0]);
59 } catch(NumberFormatException ex) {
60 }
61 }
62 }
63 }
64
65
66
67
68
69
70
71 public static ThrowableInformationPatternConverter newInstance(
72 final String[] options) {
73 return new ThrowableInformationPatternConverter(options);
74 }
75
76
77
78
79 public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
80 if (maxLines != 0) {
81 ThrowableInformation information = event.getThrowableInformation();
82
83 if (information != null) {
84 String[] stringRep = information.getThrowableStrRep();
85
86 int length = stringRep.length;
87 if (maxLines < 0) {
88 length += maxLines;
89 } else if (length > maxLines) {
90 length = maxLines;
91 }
92
93 for (int i = 0; i < length; i++) {
94 String string = stringRep[i];
95 toAppendTo.append(string).append("\n");
96 }
97 }
98 }
99 }
100
101
102
103
104
105 public boolean handlesThrowable() {
106 return true;
107 }
108 }