Skip to content

Commit c976d67

Browse files
committed
Add toggle flag for rendering message object using reflection
1 parent e7bcf04 commit c976d67

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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;
1211

1312
import java.io.Serializable;
1413
import java.lang.reflect.Field;
@@ -20,6 +19,7 @@
2019

2120
public class JSONEventLayoutV0 extends Layout {
2221

22+
private boolean renderObjectFields = false;
2323
private boolean locationInfo = false;
2424

2525
private String tags;
@@ -49,16 +49,19 @@ public static String dateFormat(long timestamp) {
4949
* in the log messages.
5050
*/
5151
public JSONEventLayoutV0() {
52-
this(true);
52+
this(true, false);
5353
}
5454

5555
/**
5656
* Creates a layout that optionally inserts location information into log messages.
5757
*
5858
* @param locationInfo whether or not to include location information in the log messages.
59+
* @param renderObjectFields whether or not to render the fields of the message object into the json when an object is logged to log4j.
60+
* Rendering the fields is done using reflection and incurs a performance cost
5961
*/
60-
public JSONEventLayoutV0(boolean locationInfo) {
62+
public JSONEventLayoutV0(boolean locationInfo, boolean renderObjectFields) {
6163
this.locationInfo = locationInfo;
64+
this.renderObjectFields = renderObjectFields;
6265
}
6366

6467
public String format(LoggingEvent loggingEvent) {
@@ -73,12 +76,15 @@ public String format(LoggingEvent loggingEvent) {
7376

7477
logstashEvent.put("@source_host", hostname);
7578
logstashEvent.put("@message", loggingEvent.getRenderedMessage());
76-
Object messageObj = loggingEvent.getMessage();
77-
if (messageObj instanceof Serializable && !(messageObj instanceof String)) {
78-
addObjectFieldData(messageObj);
79-
}
8079
logstashEvent.put("@timestamp", dateFormat(timestamp));
8180

81+
if (renderObjectFields) {
82+
Object messageObj = loggingEvent.getMessage();
83+
if (messageObj instanceof Serializable && !(messageObj instanceof String)) {
84+
addObjectFieldData(messageObj);
85+
}
86+
}
87+
8288
if (loggingEvent.getThrowableInformation() != null) {
8389
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
8490
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
@@ -160,6 +166,15 @@ public void setLocationInfo(boolean locationInfo) {
160166
this.locationInfo = locationInfo;
161167
}
162168

169+
/**
170+
* Set whether or not to render the fields of the message object into the json when an object is logged to log4j.
171+
* Rendering the fields is done using reflection and incurs a performance cost
172+
* @param renderObjectFields
173+
*/
174+
public void setRenderObjectFields(boolean renderObjectFields) {
175+
this.renderObjectFields = renderObjectFields;
176+
}
177+
163178
public void activateOptions() {
164179
activeIgnoreThrowable = ignoreThrowable;
165180
}

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
public class JSONEventLayoutV1 extends Layout {
2222

23+
private boolean renderObjectFields = false;
2324
private boolean locationInfo = false;
2425
private String customUserFields;
2526

@@ -51,15 +52,19 @@ public static String dateFormat(long timestamp) {
5152
* in the log messages.
5253
*/
5354
public JSONEventLayoutV1() {
54-
this(true);
55+
this(true, false);
5556
}
5657

5758
/**
5859
* Creates a layout that optionally inserts location information into log messages.
5960
*
6061
* @param locationInfo whether or not to include location information in the log messages.
62+
* @param renderObjectFields whether or not to render the fields of the message object into the json when an object is logged to log4j.
63+
* Rendering the fields is done using reflection and incurs a performance cost
64+
6165
*/
62-
public JSONEventLayoutV1(boolean locationInfo) {
66+
public JSONEventLayoutV1(boolean locationInfo, boolean renderObjectFields) {
67+
this.renderObjectFields = renderObjectFields;
6368
this.locationInfo = locationInfo;
6469
}
6570

@@ -107,11 +112,15 @@ public String format(LoggingEvent loggingEvent) {
107112
* Now we start injecting our own stuff.
108113
*/
109114
logstashEvent.put("source_host", hostname);
110-
Object messageObject = loggingEvent.getMessage();
111-
if (messageObject instanceof Serializable && ! (messageObject instanceof String)) {
112-
addObjectFieldData(messageObject);
113-
}
114115
logstashEvent.put("message", loggingEvent.getRenderedMessage());
116+
117+
if (renderObjectFields) {
118+
Object messageObject = loggingEvent.getMessage();
119+
if (messageObject instanceof Serializable && ! (messageObject instanceof String)) {
120+
addObjectFieldData(messageObject);
121+
}
122+
}
123+
115124
if (loggingEvent.getThrowableInformation() != null) {
116125
final ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
117126
if (throwableInformation.getThrowable().getClass().getCanonicalName() != null) {
@@ -166,6 +175,15 @@ public void setLocationInfo(boolean locationInfo) {
166175
this.locationInfo = locationInfo;
167176
}
168177

178+
/**
179+
* Set whether or not to render the fields of the message object into the json when an object is logged to log4j.
180+
* Rendering the fields is done using reflection and incurs a performance cost
181+
* @param renderObjectFields
182+
*/
183+
public void setRenderObjectFields(boolean renderObjectFields) {
184+
this.renderObjectFields = renderObjectFields;
185+
}
186+
169187
public String getUserFields() { return customUserFields; }
170188
public void setUserFields(String userFields) { this.customUserFields = userFields; }
171189

0 commit comments

Comments
 (0)