|
5 | 5 |
|
6 | 6 | package com.newrelic.logging.core;
|
7 | 7 |
|
| 8 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | + |
8 | 12 | import static com.newrelic.logging.core.LogExtensionConfig.getMaxStackSize;
|
9 | 13 |
|
10 | 14 | public class ExceptionUtil {
|
11 | 15 | public static final int MAX_STACK_SIZE_DEFAULT = 300;
|
12 |
| - public static String getErrorStack(Throwable throwable) { |
| 16 | + |
| 17 | + public static String getFullStackTrace(Throwable throwable) { |
13 | 18 | if (throwable == null) {
|
14 | 19 | return null;
|
15 | 20 | }
|
16 | 21 |
|
17 |
| - StackTraceElement[] stack = throwable.getStackTrace(); |
18 |
| - return getErrorStack(stack); |
19 |
| - } |
20 |
| - |
21 |
| - public static String getErrorStack(StackTraceElement[] stack) { |
22 |
| - return getErrorStack(stack, getMaxStackSize()); |
| 22 | + return getStackTraceStringFromFramesArray(ExceptionUtils.getStackFrames(throwable)); |
23 | 23 | }
|
24 | 24 |
|
25 |
| - public static String getErrorStack(StackTraceElement[] stack, Integer maxStackSize) { |
26 |
| - if (stack == null || stack.length == 0) { |
| 25 | + public static String transformLogbackStackTraceString(String trace) { |
| 26 | + if (trace == null || trace.length() == 0) { |
27 | 27 | return null;
|
28 | 28 | }
|
29 | 29 |
|
30 |
| - StringBuilder stackBuilder = new StringBuilder(); |
31 |
| - for(int i = 0; i < Math.min(maxStackSize, stack.length); i++) { |
32 |
| - stackBuilder.append(" at ").append(stack[i].toString()).append("\n"); |
| 30 | + return getStackTraceStringFromFramesArray(trace.split("\n")); |
| 31 | + } |
| 32 | + |
| 33 | + private static String getStackTraceStringFromFramesArray(String[] frames) { |
| 34 | + int maxStackSize = getMaxStackSize(); |
| 35 | + |
| 36 | + //We need to truncate the stacktrace based on the desired max stacktrace size, as well as remove the first line |
| 37 | + //of the returned trace (the "message"), since this is already captured in the error.message attribute. |
| 38 | + if (frames.length > maxStackSize) { |
| 39 | + frames = Arrays.copyOfRange(frames, 1, maxStackSize + 1); |
| 40 | + } else { |
| 41 | + frames = Arrays.copyOfRange(frames, 1, frames.length); |
33 | 42 | }
|
34 |
| - return stackBuilder.toString(); |
| 43 | + |
| 44 | + return String.join("\n", frames).replace("\tat", " at") + "\n"; |
35 | 45 | }
|
36 | 46 | }
|
0 commit comments