Skip to content

added layout #145

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

Closed
Closed
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> layout;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
protected Layout<ILoggingEvent> layout;
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,6 +138,14 @@ public byte[] encode(ILoggingEvent event) {
return builder.toString().getBytes(UTF_8);
}

private void serializeMessage(ILoggingEvent event, StringBuilder builder) {
if(layout == null) {
EcsJsonSerializer.serializeFormattedMessage(builder, event.getFormattedMessage());
} else {
EcsJsonSerializer.serializeFormattedMessage(builder, this.layout.doLayout(event));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
EcsJsonSerializer.serializeFormattedMessage(builder, this.layout.doLayout(event));
EcsJsonSerializer.serializeFormattedMessage(builder, this.messageLayout.doLayout(event));

}
}

/**
* Subclasses can override this to add custom fields.
* The last character in the StringBuilder will be comma when this is called.
Expand Down Expand Up @@ -198,4 +208,11 @@ public void setEventDataset(String eventDataset) {
public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
this.throwableConverter = throwableConverter;
}
public Layout<ILoggingEvent> getLayout() {
return this.layout;
}

Comment on lines +211 to +214
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public Layout<ILoggingEvent> getLayout() {
return this.layout;
}

No need for a getter

public void setLayout(Layout<ILoggingEvent> layout) {
this.layout = layout;
}
Comment on lines +215 to +217
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public void setLayout(Layout<ILoggingEvent> layout) {
this.layout = layout;
}
public void setMessageLayout(Layout<ILoggingEvent> messageLayout) {
this.messageLayout = messageLayout;
}

Add a javadoc saying this Layout will be applied specifically to format the message field based on the logging event.

}