Skip to content

Commit 4bbc529

Browse files
authored
Merge pull request #30 from longwa/feature/maxStackSize
Fix #29: Make maxStackSize configurable
2 parents 964b73e + 98bb204 commit 4bbc529

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

core/src/main/java/com/newrelic/logging/core/ExceptionUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ public static String getErrorStack(Throwable throwable) {
1717
}
1818

1919
public static String getErrorStack(StackTraceElement[] stack) {
20+
return getErrorStack(stack, MAX_STACK_SIZE);
21+
}
22+
23+
public static String getErrorStack(StackTraceElement[] stack, Integer maxStackSize) {
2024
if (stack == null || stack.length == 0) {
2125
return null;
2226
}
2327

2428
StringBuilder stackBuilder = new StringBuilder();
25-
for(int i = 0; i < Math.min(MAX_STACK_SIZE, stack.length); i++) {
29+
for(int i = 0; i < Math.min(maxStackSize, stack.length); i++) {
2630
stackBuilder.append(" at " + stack[i].toString() + "\n");
2731
}
2832
return stackBuilder.toString();

logback/src/main/java/com/newrelic/logging/logback/NewRelicEncoder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.nio.charset.StandardCharsets;
1313
import java.util.Arrays;
1414

15+
import static com.newrelic.logging.core.ExceptionUtil.MAX_STACK_SIZE;
16+
1517
/**
1618
* An {@link ch.qos.logback.core.encoder.Encoder} that will write New Relic's JSON format.
1719
*
@@ -28,7 +30,9 @@
2830
* @see <a href="https://logback.qos.ch/manual/encoders.html#interface">Logback Encoders</a>
2931
*/
3032
public class NewRelicEncoder extends EncoderBase<ILoggingEvent> {
31-
private NewRelicJsonLayout layout = new NewRelicJsonLayout();
33+
private NewRelicJsonLayout layout;
34+
35+
private Integer maxStackSize = MAX_STACK_SIZE;
3236

3337
@Override
3438
public byte[] encode(ILoggingEvent event) {
@@ -45,9 +49,14 @@ public byte[] encode(ILoggingEvent event) {
4549
@Override
4650
public void start() {
4751
super.start();
52+
layout = new NewRelicJsonLayout(maxStackSize);
4853
layout.start();
4954
}
5055

56+
public void setMaxStackSize(final Integer maxStackSize) {
57+
this.maxStackSize = maxStackSize;
58+
}
59+
5160
@Override
5261
public byte[] headerBytes() {
5362
return new byte[0];

logback/src/main/java/com/newrelic/logging/logback/NewRelicJsonLayout.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
import static com.newrelic.logging.core.ExceptionUtil.MAX_STACK_SIZE;
2323

2424
public class NewRelicJsonLayout extends LayoutBase<ILoggingEvent> {
25+
private final Integer maxStackSize;
26+
27+
public NewRelicJsonLayout() {
28+
this(MAX_STACK_SIZE);
29+
}
30+
31+
public NewRelicJsonLayout(Integer maxStackSize) {
32+
this.maxStackSize = maxStackSize;
33+
}
34+
2535
@Override
2636
public String doLayout(ILoggingEvent event) {
2737
StringWriter sw = new StringWriter();
@@ -66,12 +76,12 @@ private void writeToGenerator(ILoggingEvent event, JsonGenerator generator) thro
6676

6777
StackTraceElementProxy[] stackProxy = proxy.getStackTraceElementProxyArray();
6878
if (stackProxy != null && stackProxy.length > 0) {
69-
List<StackTraceElement> elements = new ArrayList<>(MAX_STACK_SIZE);
70-
for (int i = 0; i < MAX_STACK_SIZE && i < stackProxy.length; i++) {
79+
List<StackTraceElement> elements = new ArrayList<>(maxStackSize);
80+
for (int i = 0; i < maxStackSize && i < stackProxy.length; i++) {
7181
elements.add(stackProxy[i].getStackTraceElement());
7282
}
7383

74-
generator.writeObjectField(ElementName.ERROR_STACK, ExceptionUtil.getErrorStack(elements.toArray(new StackTraceElement[0])));
84+
generator.writeObjectField(ElementName.ERROR_STACK, ExceptionUtil.getErrorStack(elements.toArray(new StackTraceElement[0]), maxStackSize));
7585
}
7686
}
7787

0 commit comments

Comments
 (0)