|
28 | 28 | import java.io.Writer;
|
29 | 29 | import java.util.List;
|
30 | 30 | import java.util.Map;
|
| 31 | +import java.util.regex.Matcher; |
31 | 32 | import java.util.regex.Pattern;
|
32 | 33 |
|
33 | 34 | public class EcsJsonSerializer {
|
@@ -212,6 +213,10 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b
|
212 | 213 | }
|
213 | 214 |
|
214 | 215 | public static void serializeException(StringBuilder builder, String exceptionClassName, String exceptionMessage, String stackTrace, boolean stackTraceAsArray) {
|
| 216 | + serializeException(builder, exceptionClassName, exceptionMessage, (CharSequence) stackTrace, stackTraceAsArray); |
| 217 | + } |
| 218 | + |
| 219 | + public static void serializeException(StringBuilder builder, String exceptionClassName, CharSequence exceptionMessage, CharSequence stackTrace, boolean stackTraceAsArray) { |
215 | 220 | builder.append("\"error.type\":\"");
|
216 | 221 | JsonUtils.quoteAsString(exceptionClassName, builder);
|
217 | 222 | builder.append("\",");
|
@@ -258,16 +263,44 @@ public void println() {
|
258 | 263 | removeIfEndsWith(jsonBuilder, ",");
|
259 | 264 | }
|
260 | 265 |
|
261 |
| - private static void formatStackTraceAsArray(StringBuilder builder, String stackTrace) { |
| 266 | + private static void formatStackTraceAsArray(StringBuilder builder, CharSequence stackTrace) { |
262 | 267 | builder.append(NEW_LINE);
|
263 |
| - for (String line : NEW_LINE_PATTERN.split(stackTrace)) { |
264 |
| - builder.append("\t\""); |
265 |
| - JsonUtils.quoteAsString(line, builder); |
266 |
| - builder.append("\","); |
267 |
| - builder.append(NEW_LINE); |
| 268 | + |
| 269 | + // splits the stackTrace by new lines |
| 270 | + Matcher matcher = NEW_LINE_PATTERN.matcher(stackTrace); |
| 271 | + if (matcher.find()) { |
| 272 | + int index = 0; |
| 273 | + do { |
| 274 | + int start = matcher.start(); |
| 275 | + int end = matcher.end(); |
| 276 | + if (index == 0 && index == start && start == end) { |
| 277 | + // no empty leading substring included for zero-width match |
| 278 | + // at the beginning of the input char sequence. |
| 279 | + continue; |
| 280 | + } |
| 281 | + |
| 282 | + // append non-last line |
| 283 | + appendStackTraceLine(builder, stackTrace, index, start); |
| 284 | + builder.append(','); |
| 285 | + builder.append(NEW_LINE); |
| 286 | + index = end; |
| 287 | + } while (matcher.find()); |
| 288 | + |
| 289 | + int length = stackTrace.length(); |
| 290 | + if (index < length) { |
| 291 | + // append remaining line |
| 292 | + appendStackTraceLine(builder, stackTrace, index, length); |
| 293 | + } |
| 294 | + } else { |
| 295 | + // no newlines found, add entire stack trace as single element |
| 296 | + appendStackTraceLine(builder, stackTrace, 0, stackTrace.length()); |
268 | 297 | }
|
269 |
| - removeIfEndsWith(builder, NEW_LINE); |
270 |
| - removeIfEndsWith(builder, ","); |
| 298 | + } |
| 299 | + |
| 300 | + private static void appendStackTraceLine(StringBuilder builder, CharSequence stackTrace, int start, int end) { |
| 301 | + builder.append("\t\""); |
| 302 | + JsonUtils.quoteAsString(stackTrace, start, end, builder); |
| 303 | + builder.append("\""); |
271 | 304 | }
|
272 | 305 |
|
273 | 306 | public static void removeIfEndsWith(StringBuilder sb, String ending) {
|
|
0 commit comments