Lines 17-22
Link Here
|
17 |
|
17 |
|
18 |
package org.apache.log4j.pattern; |
18 |
package org.apache.log4j.pattern; |
19 |
|
19 |
|
|
|
20 |
import java.util.ArrayList; |
21 |
import java.util.Iterator; |
22 |
import java.util.List; |
23 |
|
20 |
import org.apache.log4j.spi.LoggingEvent; |
24 |
import org.apache.log4j.spi.LoggingEvent; |
21 |
import org.apache.log4j.spi.ThrowableInformation; |
25 |
import org.apache.log4j.spi.ThrowableInformation; |
22 |
|
26 |
|
Lines 32-37
Link Here
|
32 |
extends LoggingEventPatternConverter { |
36 |
extends LoggingEventPatternConverter { |
33 |
/** |
37 |
/** |
34 |
* If "short", only first line of throwable report will be formatted. |
38 |
* If "short", only first line of throwable report will be formatted. |
|
|
39 |
* If "compact", the class name and the message of the throwable and its causes |
40 |
* are concatenated and emitted in the logs. |
35 |
*/ |
41 |
*/ |
36 |
private final String option; |
42 |
private final String option; |
37 |
|
43 |
|
Lines 65-90
Link Here
|
65 |
* {@inheritDoc} |
71 |
* {@inheritDoc} |
66 |
*/ |
72 |
*/ |
67 |
public void format(final LoggingEvent event, final StringBuffer toAppendTo) { |
73 |
public void format(final LoggingEvent event, final StringBuffer toAppendTo) { |
68 |
if (!"none".equals(option)) { |
74 |
ThrowableInformation information = event.getThrowableInformation(); |
69 |
ThrowableInformation information = event.getThrowableInformation(); |
|
|
70 |
|
75 |
|
71 |
if (information != null) { |
76 |
if (information == null) { |
72 |
String[] stringRep = information.getThrowableStrRep(); |
77 |
return; |
|
|
78 |
} |
79 |
|
80 |
if ("none".equals(option)) { |
81 |
// nothing |
73 |
|
82 |
|
74 |
int length = stringRep.length; |
83 |
} else if ("short".equals(option)) { |
75 |
if ("short".equals(option)) { |
84 |
// toString() of the throwable |
76 |
length = 1; |
85 |
String msg = information.getThrowableStrRep()[0]; |
|
|
86 |
toAppendTo.append(msg).append("\n"); |
87 |
|
88 |
} else if ("compact".equals(option)){ |
89 |
// join on the same line class name and message of the throwable and its causes |
90 |
StringBuilder msg = new StringBuilder(" - "); |
91 |
List throwables = getThrowableList(information.getThrowable()); |
92 |
for (Iterator it = throwables.iterator(); it.hasNext();) { |
93 |
Throwable throwable = (Throwable)it.next(); |
94 |
msg.append(throwable.getClass().getName()).append(": ") |
95 |
.append(throwable.getMessage()); |
96 |
if (it.hasNext()) { |
97 |
msg.append(" "); |
77 |
} |
98 |
} |
78 |
|
|
|
79 |
for (int i = 0; i < length; i++) { |
80 |
String string = stringRep[i]; |
81 |
toAppendTo.append(string).append("\n"); |
82 |
} |
83 |
} |
99 |
} |
|
|
100 |
toAppendTo.append(msg); |
101 |
|
102 |
} else { |
103 |
// each line of the stack trace |
104 |
String[] stringRep = information.getThrowableStrRep(); |
105 |
for (int i = 0; i < stringRep.length; i++) { |
106 |
String string = stringRep[i]; |
107 |
toAppendTo.append(string).append("\n"); |
108 |
} |
84 |
} |
109 |
} |
85 |
} |
110 |
} |
86 |
|
111 |
|
87 |
/** |
112 |
/** |
|
|
113 |
* Builds the list of the given <tt>throwable</tt> and all its causes. |
114 |
* |
115 |
* <p>Only relies on {@link Throwable#getCause()}.</p> |
116 |
* |
117 |
* @param throwable |
118 |
* @return non <tt>null<tt> list of non <tt>null</tt> throwables |
119 |
* @see Throwable#getCause() |
120 |
*/ |
121 |
public List getThrowableList(Throwable throwable) { |
122 |
List throwables = new ArrayList(); |
123 |
while (throwable != null && throwables.contains(throwable) == false) { |
124 |
throwables.add(throwable); |
125 |
throwable = throwable.getCause(); |
126 |
} |
127 |
return throwables; |
128 |
} |
129 |
|
130 |
/** |
88 |
* This converter obviously handles throwables. |
131 |
* This converter obviously handles throwables. |
89 |
* @return true. |
132 |
* @return true. |
90 |
*/ |
133 |
*/ |