Skip to content

Commit

Permalink
Adding layout support to the logback ecs encoder (#220)
Browse files Browse the repository at this point in the history
Signed-off-by: Difrango, Ronald <[email protected]>
Co-authored-by: dgempiuc <[email protected]>
Co-authored-by: eyalkoren <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2024
1 parent 02d4625 commit 73d1988
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.encoder.EncoderBase;
import co.elastic.logging.AdditionalField;
import co.elastic.logging.EcsJsonSerializer;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class EcsEncoder extends EncoderBase<ILoggingEvent> {
private boolean includeOrigin;
private final List<AdditionalField> additionalFields = new ArrayList<AdditionalField>();
private OutputStream os;
protected Layout<ILoggingEvent> messageLayout;

@Override
public byte[] headerBytes() {
Expand Down Expand Up @@ -105,7 +107,7 @@ public byte[] encode(ILoggingEvent event) {
StringBuilder builder = new StringBuilder(256);
EcsJsonSerializer.serializeObjectStart(builder, event.getTimeStamp());
EcsJsonSerializer.serializeLogLevel(builder, event.getLevel().toString());
EcsJsonSerializer.serializeFormattedMessage(builder, event.getFormattedMessage());
serializeMessage(event, builder);
EcsJsonSerializer.serializeEcsVersion(builder);
serializeMarkers(event, builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
Expand Down Expand Up @@ -140,12 +142,21 @@ public byte[] encode(ILoggingEvent event) {
return builder.toString().getBytes(UTF_8);
}

private void serializeMessage(ILoggingEvent event, StringBuilder builder) {
if (messageLayout == null) {
EcsJsonSerializer.serializeFormattedMessage(builder, event.getFormattedMessage());
} else {
EcsJsonSerializer.serializeFormattedMessage(builder, messageLayout.doLayout(event));
}
}

/**
* 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) {}
protected void addCustomFields(ILoggingEvent event, StringBuilder builder) {
}

private void serializeMarkers(ILoggingEvent event, StringBuilder builder) {
Marker marker = event.getMarker();
Expand Down Expand Up @@ -210,4 +221,13 @@ public void setEventDataset(String eventDataset) {
public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
this.throwableConverter = throwableConverter;
}

/**
* The supplied Layout will be applied specifically to format the <code>message</code> field based on the logging event.
*
* @param messageLayout
*/
public void setMessageLayout(Layout<ILoggingEvent> messageLayout) {
this.messageLayout = messageLayout;
}
}

0 comments on commit 73d1988

Please sign in to comment.