Skip to content

Commit

Permalink
Closes #110
Browse files Browse the repository at this point in the history
Add an option to override json serialization
  • Loading branch information
vpispanen committed Sep 27, 2024
1 parent 899a36d commit 227a9af
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,7 @@ public byte[] encode(final ILoggingEvent event) {
collectAdditionalFields(event)
);

final var sb = gelfMessage.toJSON();

if (appendNewline) {
sb.append(System.lineSeparator());
}

return sb.toString().getBytes(StandardCharsets.UTF_8);
return gelfMessageToJson(gelfMessage);
}

protected GelfMessage buildGelfMessage(final long timestamp, final int logLevel, final String shortMessage,
Expand Down Expand Up @@ -472,6 +466,23 @@ private void addFieldMapperData(final ILoggingEvent event, final Map<String, Obj
}
}


/**
* Allow subclasses to customize the message before it is converted to String.
*
* @param gelfMessage the GELF message to serialize.
* @return the serialized GELF message (in JSON format).
*/
protected byte[] gelfMessageToJson(final GelfMessage gelfMessage) {
final var sb = gelfMessage.toJSON();

if (appendNewline) {
sb.append(System.lineSeparator());
}

return sb.toString().getBytes(StandardCharsets.UTF_8);
}

@SuppressWarnings({"PMD.ReturnEmptyArrayRatherThanNull", "PMD.ReturnEmptyCollectionRatherThanNull"})
@Override
public byte[] footerBytes() {
Expand Down

0 comments on commit 227a9af

Please sign in to comment.