|
8 | 8 | import org.apache.log4j.spi.LocationInfo; |
9 | 9 | import org.apache.log4j.spi.LoggingEvent; |
10 | 10 | import org.apache.log4j.spi.ThrowableInformation; |
| 11 | +import sun.util.logging.resources.logging; |
11 | 12 |
|
| 13 | +import java.io.Serializable; |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Method; |
12 | 17 | import java.util.HashMap; |
13 | 18 | import java.util.Map; |
14 | 19 | import java.util.TimeZone; |
@@ -68,6 +73,10 @@ public String format(LoggingEvent loggingEvent) { |
68 | 73 |
|
69 | 74 | logstashEvent.put("@source_host", hostname); |
70 | 75 | logstashEvent.put("@message", loggingEvent.getRenderedMessage()); |
| 76 | + Object messageObj = loggingEvent.getMessage(); |
| 77 | + if (messageObj instanceof Serializable && !(messageObj instanceof String)) { |
| 78 | + addObjectFieldData(messageObj); |
| 79 | + } |
71 | 80 | logstashEvent.put("@timestamp", dateFormat(timestamp)); |
72 | 81 |
|
73 | 82 | if (loggingEvent.getThrowableInformation() != null) { |
@@ -103,6 +112,32 @@ public String format(LoggingEvent loggingEvent) { |
103 | 112 | return logstashEvent.toString() + "\n"; |
104 | 113 | } |
105 | 114 |
|
| 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 | + |
106 | 141 | public boolean ignoresThrowable() { |
107 | 142 | return ignoreThrowable; |
108 | 143 | } |
|
0 commit comments