Skip to content

Commit

Permalink
Implement ability to configure a throwable converter
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmcfadyensky committed Mar 18, 2021
1 parent 80e1f44 commit 8d271be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -34,7 +34,7 @@ public class EcsJsonSerializer {

private static final TimestampSerializer TIMESTAMP_SERIALIZER = new TimestampSerializer();
private static final ThreadLocal<StringBuilder> messageStringBuilder = new ThreadLocal<StringBuilder>();
private static final String NEW_LINE = System.getProperty("line.separator");
private static final String NEW_LINE = System.getProperty("line.separator");
private static final Pattern NEW_LINE_PATTERN = Pattern.compile("\\n");

public static CharSequence toNullSafeString(final CharSequence s) {
Expand Down Expand Up @@ -79,6 +79,7 @@ public static void serializeThreadId(StringBuilder builder, long threadId) {
builder.append(threadId);
builder.append(",");
}

public static void serializeFormattedMessage(StringBuilder builder, String message) {
builder.append("\"message\":\"");
JsonUtils.quoteAsString(message, builder);
Expand Down Expand Up @@ -202,9 +203,12 @@ public static void serializeException(StringBuilder builder, String exceptionCla
builder.append("\"error.type\":\"");
JsonUtils.quoteAsString(exceptionClassName, builder);
builder.append("\",");
builder.append("\"error.message\":\"");
JsonUtils.quoteAsString(exceptionMessage, builder);
builder.append("\",");

if (exceptionMessage != null) {
builder.append("\"error.message\":\"");
JsonUtils.quoteAsString(exceptionMessage, builder);
builder.append("\",");
}
if (stackTraceAsArray) {
builder.append("\"error.stack_trace\":[").append(NEW_LINE);
for (String line : NEW_LINE_PATTERN.split(stackTrace)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -24,10 +24,10 @@
*/
package co.elastic.logging.logback;

import ch.qos.logback.classic.pattern.ThrowableHandlingConverter;
import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
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.encoder.EncoderBase;
import co.elastic.logging.EcsJsonSerializer;
import co.elastic.logging.AdditionalField;
Expand All @@ -47,7 +47,7 @@ public class EcsEncoder extends EncoderBase<ILoggingEvent> {
private String serviceName;
private String eventDataset;
private boolean includeMarkers = false;
private ThrowableProxyConverter throwableProxyConverter;
private ThrowableHandlingConverter throwableConverter = new ThrowableProxyConverter();
private boolean includeOrigin;
private final List<AdditionalField> additionalFields = new ArrayList<AdditionalField>();
private OutputStream os;
Expand All @@ -60,8 +60,7 @@ public byte[] headerBytes() {
@Override
public void start() {
super.start();
throwableProxyConverter = new ThrowableProxyConverter();
throwableProxyConverter.start();
throwableConverter.start();
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
}
/**
Expand Down Expand Up @@ -113,10 +112,8 @@ public byte[] encode(ILoggingEvent event) {
}
}
IThrowableProxy throwableProxy = event.getThrowableProxy();
if (throwableProxy instanceof ThrowableProxy) {
EcsJsonSerializer.serializeException(builder, ((ThrowableProxy) throwableProxy).getThrowable(), stackTraceAsArray);
} else if (throwableProxy != null) {
EcsJsonSerializer.serializeException(builder, throwableProxy.getClassName(), throwableProxy.getMessage(), throwableProxyConverter.convert(event), stackTraceAsArray);
if (throwableProxy != null) {
EcsJsonSerializer.serializeException(builder, throwableProxy.getClassName(), throwableProxy.getMessage(), throwableConverter.convert(event), stackTraceAsArray);
}
EcsJsonSerializer.serializeObjectEnd(builder);
// all these allocations kinda hurt
Expand Down Expand Up @@ -171,4 +168,7 @@ public void setEventDataset(String eventDataset) {
this.eventDataset = eventDataset;
}

public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
this.throwableConverter = throwableConverter;
}
}

0 comments on commit 8d271be

Please sign in to comment.