Skip to content

Commit

Permalink
Allow subclasses to insert custom fields
Browse files Browse the repository at this point in the history
Protected method that subclasses can override to insert arbitrary custom fields in the logback encoder. #147
  • Loading branch information
echatman authored Oct 18, 2021
1 parent c91dd98 commit 3ca0dca
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public byte[] encode(ILoggingEvent event) {
EcsJsonSerializer.serializeOrigin(builder, callerData[0]);
}
}
// 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.
addCustomFields(event, builder);
IThrowableProxy throwableProxy = event.getThrowableProxy();
if (throwableProxy instanceof ThrowableProxy) {
EcsJsonSerializer.serializeException(builder, ((ThrowableProxy) throwableProxy).getThrowable(), stackTraceAsArray);
Expand All @@ -125,6 +127,13 @@ public byte[] encode(ILoggingEvent event) {
return builder.toString().getBytes(UTF_8);
}

/**
* Subclasses can override this to add custom fields.
* The last character in the StringBuilder will be comma when this is called.
* You must add a comma after each custom field.
*/
protected void addCustomFields(ILoggingEvent event, StringBuilder builder) {}

private void serializeMarkers(ILoggingEvent event, StringBuilder builder) {
Marker marker = event.getMarker();
if (includeMarkers && marker != null) {
Expand Down

0 comments on commit 3ca0dca

Please sign in to comment.