Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding layout support to the logback ecs encoder #220

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -54,6 +55,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 @@ -103,7 +105,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 @@ -136,12 +138,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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved per the PR comments.

}
}

/**
* 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 @@ -198,4 +209,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;
}
}
Loading