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

Preventing NPE after (de)serialization of a LoggingEvent #48

Open
wants to merge 2 commits 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.iml
target/*
.idea
.settings/*
.project
.classpath
12 changes: 7 additions & 5 deletions src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ public String format(LoggingEvent loggingEvent) {

if (loggingEvent.getThrowableInformation() != null) {
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
}
if (throwableInformation.getThrowable().getMessage() != null) {
exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
if (throwableInformation.getThrowable() != null) {
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
exceptionInformation.put("exception_class", throwableInformation.getThrowable().getClass().getCanonicalName());
}
if (throwableInformation.getThrowable().getMessage() != null) {
exceptionInformation.put("exception_message", throwableInformation.getThrowable().getMessage());
}
}
if (throwableInformation.getThrowableStrRep() != null) {
String stackTrace = StringUtils.join(throwableInformation.getThrowableStrRep(), "\n");
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import junit.framework.Assert;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

import org.apache.log4j.*;
import org.apache.log4j.or.ObjectRenderer;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -291,4 +297,55 @@ public void testDateFormat() {
long timestamp = 1364844991207L;
Assert.assertEquals("format does not produce expected output", "2013-04-01T19:36:31.207Z", JSONEventLayoutV1.dateFormat(timestamp));
}

@Test
public void testJSONEventLayoutAfterSerialization() {
logger.removeAppender(appender);

MockAppenderV1 serializingAppender = new MockAppenderV1(new JSONEventLayoutV1()) {
@Override
protected void append(LoggingEvent event) {
LoggingEvent eventAfterSerialization = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(event);
oos.flush();

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
eventAfterSerialization = (LoggingEvent) ois.readObject();

Assert.assertNotNull("The throwable information should still exist after serialization", eventAfterSerialization.getThrowableInformation());
Assert.assertNull("The transient throwable should no longer exist after serialization", eventAfterSerialization.getThrowableInformation().getThrowable());

} catch (Exception ex) {
Assert.fail("Unexpected error while serializing the logging event: " + ex.getMessage());
}
super.append(eventAfterSerialization);
}
};

logger.addAppender(serializingAppender);

try {
logger.error("The throwable for this event will be removed because of serialization", new RuntimeException("For stacktrace purpose"));
} catch (Exception ex) {
Assert.fail("Error while processing logging event after serialization: " + ex.getMessage());
} finally {
logger.removeAppender(serializingAppender);
logger.addAppender(appender);
}

String message = appender.getMessages()[0];

Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
JSONObject exceptionInformation = (JSONObject) jsonObject.get("exception");

Assert.assertNull("The exception class should not have been read from a serialized logging event", exceptionInformation.get("exception_class"));
Assert.assertNull("The exception message should not have been read from a serialized logging event", exceptionInformation.get("exception_message"));
Assert.assertNotNull("Stacktrace should still be available after serialization", exceptionInformation.get("stacktrace"));
Assert.assertTrue(exceptionInformation.get("stacktrace").toString().startsWith("java.lang.RuntimeException: For stacktrace purpose"));
}
}