Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public static String getErrorStack(Throwable throwable) {
}

public static String getErrorStack(StackTraceElement[] stack) {
return getErrorStack(stack, MAX_STACK_SIZE);
}

public static String getErrorStack(StackTraceElement[] stack, Integer maxStackSize) {
if (stack == null || stack.length == 0) {
return null;
}

StringBuilder stackBuilder = new StringBuilder();
for(int i = 0; i < Math.min(MAX_STACK_SIZE, stack.length); i++) {
for(int i = 0; i < Math.min(maxStackSize, stack.length); i++) {
stackBuilder.append(" at " + stack[i].toString() + "\n");
}
return stackBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static com.newrelic.logging.core.ExceptionUtil.MAX_STACK_SIZE;

/**
* An {@link ch.qos.logback.core.encoder.Encoder} that will write New Relic's JSON format.
*
Expand All @@ -28,7 +30,9 @@
* @see <a href="https://logback.qos.ch/manual/encoders.html#interface">Logback Encoders</a>
*/
public class NewRelicEncoder extends EncoderBase<ILoggingEvent> {
private NewRelicJsonLayout layout = new NewRelicJsonLayout();
private NewRelicJsonLayout layout;

private Integer maxStackSize = MAX_STACK_SIZE;

@Override
public byte[] encode(ILoggingEvent event) {
Expand All @@ -45,9 +49,14 @@ public byte[] encode(ILoggingEvent event) {
@Override
public void start() {
super.start();
layout = new NewRelicJsonLayout(maxStackSize);
layout.start();
}

public void setMaxStackSize(final Integer maxStackSize) {
this.maxStackSize = maxStackSize;
}

@Override
public byte[] headerBytes() {
return new byte[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
import static com.newrelic.logging.core.ExceptionUtil.MAX_STACK_SIZE;

public class NewRelicJsonLayout extends LayoutBase<ILoggingEvent> {
private final Integer maxStackSize;

public NewRelicJsonLayout() {
this(MAX_STACK_SIZE);
}

public NewRelicJsonLayout(Integer maxStackSize) {
this.maxStackSize = maxStackSize;
}

@Override
public String doLayout(ILoggingEvent event) {
StringWriter sw = new StringWriter();
Expand Down Expand Up @@ -67,12 +77,12 @@ private void writeToGenerator(ILoggingEvent event, JsonGenerator generator) thro

StackTraceElementProxy[] stackProxy = proxy.getStackTraceElementProxyArray();
if (stackProxy != null && stackProxy.length > 0) {
List<StackTraceElement> elements = new ArrayList<>(MAX_STACK_SIZE);
for (int i = 0; i < MAX_STACK_SIZE && i < stackProxy.length; i++) {
List<StackTraceElement> elements = new ArrayList<>(maxStackSize);
for (int i = 0; i < maxStackSize && i < stackProxy.length; i++) {
elements.add(stackProxy[i].getStackTraceElement());
}

generator.writeObjectField(ElementName.ERROR_STACK, ExceptionUtil.getErrorStack(elements.toArray(new StackTraceElement[0])));
generator.writeObjectField(ElementName.ERROR_STACK, ExceptionUtil.getErrorStack(elements.toArray(new StackTraceElement[0]), maxStackSize));
}
}

Expand Down