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

Add message object's fields to top level fields #62

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
45 changes: 45 additions & 0 deletions src/main/java/net/logstash/log4j/JSONEventLayoutV0.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
Expand Down Expand Up @@ -68,6 +71,21 @@ public String format(LoggingEvent loggingEvent) {

logstashEvent.put("@source_host", hostname);
logstashEvent.put("@message", loggingEvent.getRenderedMessage());

/**
* Check if msg is Map, if Map add fields to log events
*/
Object messageObj = loggingEvent.getMessage();
if (messageObj instanceof Serializable && ! (messageObj instanceof String)) {
try {
addObjectFieldData(messageObj);
} catch (Exception ex) {
logstashEvent.put("message", loggingEvent.getRenderedMessage());
}
} else {
logstashEvent.put("message", loggingEvent.getRenderedMessage());
}

logstashEvent.put("@timestamp", dateFormat(timestamp));

if (loggingEvent.getThrowableInformation() != null) {
Expand Down Expand Up @@ -95,6 +113,11 @@ public String format(LoggingEvent loggingEvent) {

addFieldData("loggerName", loggingEvent.getLoggerName());
addFieldData("mdc", mdc);
try {
addObjectFieldData(mdc);
} catch (Exception e) {
}

addFieldData("ndc", ndc);
addFieldData("level", loggingEvent.getLevel().toString());
addFieldData("threadName", threadName);
Expand Down Expand Up @@ -134,4 +157,26 @@ private void addFieldData(String keyname, Object keyval) {
fieldData.put(keyname, keyval);
}
}

private void addObjectFieldData(Object messageObj) throws Exception {
if (messageObj instanceof Map) {
Map messageObjMap = (Map) messageObj;
for (Object key : messageObjMap.keySet()) {
addFieldData(key.toString(), messageObjMap.get(key));
}
} else {
Field[] fields = messageObj.getClass().getDeclaredFields();

for (Field field : fields) {
addFieldData(field.getName(), field.get(messageObj));
}
Method[] methods = messageObj.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().startsWith("get")) {
addFieldData(m.getName().substring(3), m.invoke(messageObj));
}
}
}
}

}
44 changes: 43 additions & 1 deletion src/main/java/net/logstash/log4j/JSONEventLayoutV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
Expand Down Expand Up @@ -103,7 +106,20 @@ public String format(LoggingEvent loggingEvent) {
* Now we start injecting our own stuff.
*/
logstashEvent.put("source_host", hostname);
logstashEvent.put("message", loggingEvent.getRenderedMessage());

/**
* Check if msg is Map, if Map add fields to log events
*/
Object messageObj = loggingEvent.getMessage();
if (messageObj instanceof Serializable && ! (messageObj instanceof String)) {
try {
addObjectFieldData(messageObj);
} catch (Exception ex) {
logstashEvent.put("message", loggingEvent.getRenderedMessage());
}
} else {
logstashEvent.put("message", loggingEvent.getRenderedMessage());
}

if (loggingEvent.getThrowableInformation() != null) {
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
Expand All @@ -130,6 +146,11 @@ public String format(LoggingEvent loggingEvent) {

addEventData("logger_name", loggingEvent.getLoggerName());
addEventData("mdc", mdc);
try {
addObjectFieldData(mdc);
} catch (Exception e) {
}

addEventData("ndc", ndc);
addEventData("level", loggingEvent.getLevel().toString());
addEventData("thread_name", threadName);
Expand Down Expand Up @@ -184,4 +205,25 @@ private void addEventData(String keyname, Object keyval) {
logstashEvent.put(keyname, keyval);
}
}

private void addObjectFieldData(Object messageObj) throws Exception {
if (messageObj instanceof Map) {
Map messageObjMap = (Map) messageObj;
for (Object key : messageObjMap.keySet()) {
addEventData(key.toString(), messageObjMap.get(key));
}
} else {
Field[] fields = messageObj.getClass().getDeclaredFields();

for (Field field : fields) {
addEventData(field.getName(), field.get(messageObj));
}
Method[] methods = messageObj.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().startsWith("get")) {
addEventData(m.getName().substring(3), m.invoke(messageObj));
}
}
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/net/logstash/log4j/JSONEventLayoutV0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import org.junit.Ignore;
import org.junit.Test;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
* User: jvincent
Expand Down Expand Up @@ -99,6 +103,7 @@ public void testJSONEventLayoutHasMDC() {
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
JSONObject mdcData = (JSONObject) atFields.get("mdc");

Assert.assertEquals("MDC is wrong","bar", atFields.get("foo"));
Assert.assertEquals("MDC is wrong","bar", mdcData.get("foo"));
}

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

@Test
public void testMessageObjectRendering() {
logger.info(new Serializable() {
String test = "TEST";
int testNum = 1123;
});
String message = appender.getMessages()[0];
System.out.println(message);
Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
Assert.assertTrue(atFields.containsKey("testNum"));
Assert.assertTrue(atFields.get("testNum") instanceof Integer);
Assert.assertEquals(atFields.get("testNum"), 1123);
}

@Test
public void testMessageObjectRenderingMap() {
Map testMap = new HashMap<String, Object>();
testMap.put("test", "TEST");
testMap.put("testNum", 12345);

logger.info(testMap);
String message = appender.getMessages()[0];
Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
JSONObject atFields = (JSONObject) jsonObject.get("@fields");
Assert.assertTrue(atFields.containsKey("testNum"));
Assert.assertTrue(atFields.get("testNum") instanceof Integer);
Assert.assertEquals(atFields.get("testNum"), 12345);
}
}
35 changes: 35 additions & 0 deletions src/test/java/net/logstash/log4j/JSONEventLayoutV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.junit.Ignore;
import org.junit.Test;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
* Created with IntelliJ IDEA.
Expand Down Expand Up @@ -165,6 +167,7 @@ public void testJSONEventLayoutHasMDC() {
JSONObject jsonObject = (JSONObject) obj;
JSONObject mdc = (JSONObject) jsonObject.get("mdc");

Assert.assertEquals("MDC is wrong","bar", jsonObject.get("foo"));
Assert.assertEquals("MDC is wrong","bar", mdc.get("foo"));
}

Expand Down Expand Up @@ -291,4 +294,36 @@ 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 testMessageObjectRendering() {
JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout();
logger.info(new Serializable() {
String test = "TEST";
int testNum = 12345;
});
String message = appender.getMessages()[0];
System.out.println(message);
Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
Assert.assertTrue(jsonObject.containsKey("testNum"));
Assert.assertTrue(jsonObject.get("testNum") instanceof Integer);
Assert.assertEquals(jsonObject.get("testNum"), 12345);
}

@Test
public void testMessageObjectRenderingMap() {
JSONEventLayoutV1 layout = (JSONEventLayoutV1) appender.getLayout();
Map testMap = new HashMap<String, Object>();
testMap.put("test", "TEST");
testMap.put("testNum", 12345);

logger.info(testMap);
String message = appender.getMessages()[0];
Object obj = JSONValue.parse(message);
JSONObject jsonObject = (JSONObject) obj;
Assert.assertTrue(jsonObject.containsKey("testNum"));
Assert.assertTrue(jsonObject.get("testNum") instanceof Integer);
Assert.assertEquals(jsonObject.get("testNum"), 12345);
}
}