-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ThrowableProxyConverter.java
201 lines (170 loc) · 6.41 KB
/
ThrowableProxyConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2013, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.boolex.EvaluationException;
import ch.qos.logback.core.boolex.EventEvaluator;
import ch.qos.logback.core.status.ErrorStatus;
/**
* Add a stack trace in case the event contains a Throwable.
*
* @author Ceki Gülcü
*/
public class ThrowableProxyConverter extends ThrowableHandlingConverter {
protected static final int BUILDER_CAPACITY = 2048;
int lengthOption;
List<EventEvaluator<ILoggingEvent>> evaluatorList = null;
int errorCount = 0;
@SuppressWarnings("unchecked")
public void start() {
String lengthStr = getFirstOption();
if (lengthStr == null) {
lengthOption = Integer.MAX_VALUE;
} else {
lengthStr = lengthStr.toLowerCase();
if ("full".equals(lengthStr)) {
lengthOption = Integer.MAX_VALUE;
} else if ("short".equals(lengthStr)) {
lengthOption = 1;
} else {
try {
lengthOption = Integer.parseInt(lengthStr);
} catch (NumberFormatException nfe) {
addError("Could not parse [" + lengthStr + "] as an integer");
lengthOption = Integer.MAX_VALUE;
}
}
}
final List optionList = getOptionList();
if (optionList != null && optionList.size() > 1) {
final int optionListSize = optionList.size();
for (int i = 1; i < optionListSize; i++) {
String evaluatorStr = (String) optionList.get(i);
Context context = getContext();
Map evaluatorMap = (Map) context.getObject(CoreConstants.EVALUATOR_MAP);
EventEvaluator<ILoggingEvent> ee = (EventEvaluator<ILoggingEvent>) evaluatorMap
.get(evaluatorStr);
addEvaluator(ee);
}
}
super.start();
}
private void addEvaluator(EventEvaluator<ILoggingEvent> ee) {
if (evaluatorList == null) {
evaluatorList = new ArrayList<EventEvaluator<ILoggingEvent>>();
}
evaluatorList.add(ee);
}
public void stop() {
evaluatorList = null;
super.stop();
}
protected void extraData(StringBuilder builder, StackTraceElementProxy step) {
// nop
}
public String convert(ILoggingEvent event) {
IThrowableProxy tp = event.getThrowableProxy();
if (tp == null) {
return CoreConstants.EMPTY_STRING;
}
// an evaluator match will cause stack printing to be skipped
if (evaluatorList != null) {
boolean printStack = true;
for (int i = 0; i < evaluatorList.size(); i++) {
EventEvaluator<ILoggingEvent> ee = evaluatorList.get(i);
try {
if (ee.evaluate(event)) {
printStack = false;
break;
}
} catch (EvaluationException eex) {
errorCount++;
if (errorCount < CoreConstants.MAX_ERROR_COUNT) {
addError("Exception thrown for evaluator named [" + ee.getName()
+ "]", eex);
} else if (errorCount == CoreConstants.MAX_ERROR_COUNT) {
ErrorStatus errorStatus = new ErrorStatus(
"Exception thrown for evaluator named [" + ee.getName() + "].",
this, eex);
errorStatus.add(new ErrorStatus(
"This was the last warning about this evaluator's errors."
+ "We don't want the StatusManager to get flooded.", this));
addStatus(errorStatus);
}
}
}
if (!printStack) {
return CoreConstants.EMPTY_STRING;
}
}
return throwableProxyToString(tp);
}
protected String throwableProxyToString(IThrowableProxy tp) {
StringBuilder sb = new StringBuilder(BUILDER_CAPACITY);
recursiveAppend(sb, null, ThrowableProxyUtil.REGULAR_EXCEPTION_INDENT, tp);
return sb.toString();
}
private void recursiveAppend(StringBuilder sb, String prefix, int indent, IThrowableProxy tp) {
if(tp == null)
return;
subjoinFirstLine(sb, prefix, indent, tp);
sb.append(CoreConstants.LINE_SEPARATOR);
subjoinSTEPArray(sb, indent, tp);
IThrowableProxy[] suppressed = tp.getSuppressed();
if(suppressed != null) {
for(IThrowableProxy current : suppressed) {
recursiveAppend(sb, CoreConstants.SUPPRESSED, indent + ThrowableProxyUtil.SUPPRESSED_EXCEPTION_INDENT, current);
}
}
recursiveAppend(sb, CoreConstants.CAUSED_BY, indent, tp.getCause());
}
private void subjoinFirstLine(StringBuilder buf, String prefix, int indent, IThrowableProxy tp) {
ThrowableProxyUtil.indent(buf, indent - 1);
if (prefix != null) {
buf.append(prefix);
}
subjoinExceptionMessage(buf, tp);
}
private void subjoinExceptionMessage(StringBuilder buf, IThrowableProxy tp) {
buf.append(tp.getClassName()).append(": ").append(tp.getMessage());
}
private void subjoinSTEPArray(StringBuilder buf, int indent, IThrowableProxy tp) {
StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
int commonFrames = tp.getCommonFrames();
boolean unrestrictedPrinting = lengthOption > stepArray.length;
int maxIndex = (unrestrictedPrinting) ? stepArray.length : lengthOption;
if (commonFrames > 0 && unrestrictedPrinting) {
maxIndex -= commonFrames;
}
for (int i = 0; i < maxIndex; i++) {
ThrowableProxyUtil.indent(buf, indent);
buf.append(stepArray[i]);
extraData(buf, stepArray[i]); // allow other data to be added
buf.append(CoreConstants.LINE_SEPARATOR);
}
if (commonFrames > 0 && unrestrictedPrinting) {
ThrowableProxyUtil.indent(buf, indent);
buf.append("... ").append(tp.getCommonFrames()).append(
" common frames omitted").append(CoreConstants.LINE_SEPARATOR);
}
}
}