Skip to content

Commit e7bcf04

Browse files
committed
Add support for message objects, render object to json message
1 parent 6fe6dcd commit e7bcf04

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/main/java/net/logstash/log4j/JSONEventLayoutV0.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
import org.apache.log4j.spi.LocationInfo;
99
import org.apache.log4j.spi.LoggingEvent;
1010
import org.apache.log4j.spi.ThrowableInformation;
11+
import sun.util.logging.resources.logging;
1112

13+
import java.io.Serializable;
14+
import java.lang.reflect.Field;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.lang.reflect.Method;
1217
import java.util.HashMap;
1318
import java.util.Map;
1419
import java.util.TimeZone;
@@ -68,6 +73,10 @@ public String format(LoggingEvent loggingEvent) {
6873

6974
logstashEvent.put("@source_host", hostname);
7075
logstashEvent.put("@message", loggingEvent.getRenderedMessage());
76+
Object messageObj = loggingEvent.getMessage();
77+
if (messageObj instanceof Serializable && !(messageObj instanceof String)) {
78+
addObjectFieldData(messageObj);
79+
}
7180
logstashEvent.put("@timestamp", dateFormat(timestamp));
7281

7382
if (loggingEvent.getThrowableInformation() != null) {
@@ -103,6 +112,32 @@ public String format(LoggingEvent loggingEvent) {
103112
return logstashEvent.toString() + "\n";
104113
}
105114

115+
private void addObjectFieldData(Object messageObj) {
116+
Field[] fields = messageObj.getClass().getFields();
117+
Object value = null;
118+
119+
for(Field f : fields) {
120+
try {
121+
value = f.get(messageObj);
122+
if (value != null) fieldData.put(f.getName(), value);
123+
} catch (IllegalAccessException e) {
124+
}
125+
}
126+
Method[] methods = messageObj.getClass().getMethods();
127+
for(Method m : methods)
128+
{
129+
if(m.getName().startsWith("get"))
130+
{
131+
try {
132+
value = m.invoke(messageObj);
133+
} catch (IllegalAccessException e) {
134+
} catch (InvocationTargetException e) {
135+
}
136+
if (value != null) fieldData.put(m.getName().substring(3), value);
137+
}
138+
}
139+
}
140+
106141
public boolean ignoresThrowable() {
107142
return ignoreThrowable;
108143
}

src/main/java/net/logstash/log4j/JSONEventLayoutV1.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import org.apache.log4j.spi.LoggingEvent;
1111
import org.apache.log4j.spi.ThrowableInformation;
1212

13+
import java.io.Serializable;
14+
import java.lang.reflect.Field;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.lang.reflect.Method;
1317
import java.util.HashMap;
1418
import java.util.Map;
1519
import java.util.TimeZone;
@@ -103,8 +107,11 @@ public String format(LoggingEvent loggingEvent) {
103107
* Now we start injecting our own stuff.
104108
*/
105109
logstashEvent.put("source_host", hostname);
110+
Object messageObject = loggingEvent.getMessage();
111+
if (messageObject instanceof Serializable && ! (messageObject instanceof String)) {
112+
addObjectFieldData(messageObject);
113+
}
106114
logstashEvent.put("message", loggingEvent.getRenderedMessage());
107-
108115
if (loggingEvent.getThrowableInformation() != null) {
109116
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
110117
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
@@ -184,4 +191,30 @@ private void addEventData(String keyname, Object keyval) {
184191
logstashEvent.put(keyname, keyval);
185192
}
186193
}
194+
195+
private void addObjectFieldData(Object messageObj) {
196+
Field[] fields = messageObj.getClass().getFields();
197+
Object value = null;
198+
199+
for(Field f : fields) {
200+
try {
201+
value = f.get(messageObj);
202+
if (value != null) addEventData(f.getName(), value);
203+
} catch (IllegalAccessException e) {
204+
}
205+
}
206+
Method[] methods = messageObj.getClass().getMethods();
207+
for(Method m : methods)
208+
{
209+
if(m.getName().startsWith("get"))
210+
{
211+
try {
212+
value = m.invoke(messageObj);
213+
} catch (IllegalAccessException e) {
214+
} catch (InvocationTargetException e) {
215+
}
216+
if (value != null) addEventData(m.getName().substring(3), value);
217+
}
218+
}
219+
}
187220
}

0 commit comments

Comments
 (0)