Skip to content

Commit 2595ee9

Browse files
authored
Allow subclasses to insert custom fields (#151)
Protected method that subclasses can override to insert arbitrary custom fields in the logback encoder closes #147
1 parent 5bfada3 commit 2595ee9

File tree

1 file changed

+9
-0
lines changed
  • logback-ecs-encoder/src/main/java/co/elastic/logging/logback

1 file changed

+9
-0
lines changed

logback-ecs-encoder/src/main/java/co/elastic/logging/logback/EcsEncoder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public byte[] encode(ILoggingEvent event) {
114114
EcsJsonSerializer.serializeOrigin(builder, callerData[0]);
115115
}
116116
}
117+
// Allow subclasses to add custom fields. Calling this before the throwable serialization so we don't need to check for presence/absence of an ending comma.
118+
addCustomFields(event, builder);
117119
IThrowableProxy throwableProxy = event.getThrowableProxy();
118120
if (throwableProxy instanceof ThrowableProxy) {
119121
EcsJsonSerializer.serializeException(builder, ((ThrowableProxy) throwableProxy).getThrowable(), stackTraceAsArray);
@@ -125,6 +127,13 @@ public byte[] encode(ILoggingEvent event) {
125127
return builder.toString().getBytes(UTF_8);
126128
}
127129

130+
/**
131+
* Subclasses can override this to add custom fields.
132+
* The last character in the StringBuilder will be comma when this is called.
133+
* You must add a comma after each custom field.
134+
*/
135+
protected void addCustomFields(ILoggingEvent event, StringBuilder builder) {}
136+
128137
private void serializeMarkers(ILoggingEvent event, StringBuilder builder) {
129138
Marker marker = event.getMarker();
130139
if (includeMarkers && marker != null) {

0 commit comments

Comments
 (0)