Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shorten stacktrace via user fields #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
Expand Down Expand Up @@ -115,6 +116,16 @@ public String format(LoggingEvent loggingEvent) {
}
if (throwableInformation.getThrowableStrRep() != null) {
String stackTrace = StringUtils.join(throwableInformation.getThrowableStrRep(), "\n");
/**
* to shorten stack trace via user fields in the form "stacktrace:10", where 10 - the number of stacktrace lines to print
*/
if (logstashEvent.get("stacktrace") != null) {
int stacktraceLinesLimit = Integer.parseInt((String) logstashEvent.get("stacktrace"));
if (stacktraceLinesLimit < throwableInformation.getThrowableStrRep().length)
stackTrace = StringUtils.join(Arrays.copyOfRange(throwableInformation.getThrowableStrRep(), 0, stacktraceLinesLimit), "\n");
}


exceptionInformation.put("stacktrace", stackTrace);
}
addEventData("exception", exceptionInformation);
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import junit.framework.Assert;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.*;
import org.apache.log4j.or.ObjectRenderer;
import org.junit.After;
Expand All @@ -29,6 +30,7 @@ public class JSONEventLayoutV1Test {
static final String userFieldsSingle = new String("field1:value1");
static final String userFieldsMulti = new String("field2:value2,field3:value3");
static final String userFieldsSingleProperty = new String("field1:propval1");
static final String userFieldsStacktrace = new String("stacktrace:5");

static final String[] logstashFields = new String[]{
"message",
Expand Down Expand Up @@ -197,6 +199,27 @@ public void testJSONEventLayoutExceptions() {
Assert.assertEquals("Exception exception message", exceptionMessage, exceptionInformation.get("exception_message"));
}

@Test
public void testJSONEventLayoutExceptionsWithUserFields() {

JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout();
String prevUserData = layout.getUserFields();
layout.setUserFields(userFieldsStacktrace);

String exceptionMessage = new String("shits on fire, yo");
logger.fatal("uh-oh", new IllegalArgumentException(exceptionMessage));
String message = appender.getMessages()[0];
Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
JSONObject exceptionInformation = (JSONObject) jsonObject.get("exception");

Assert.assertEquals("Exception class missing", "java.lang.IllegalArgumentException", exceptionInformation.get("exception_class"));
Assert.assertEquals("Exception exception message", exceptionMessage, exceptionInformation.get("exception_message"));
Assert.assertEquals("The number of lines in stacktrace is correct", StringUtils.countMatches((String) exceptionInformation.get("stacktrace"), "\n"), Integer.parseInt(userFieldsStacktrace.substring(userFieldsStacktrace.indexOf(":")+1))-1);

layout.setUserFields(prevUserData);
}

@Test
public void testJSONEventLayoutHasClassName() {
logger.warn("warning dawg");
Expand Down