Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>io.avaje</groupId>
<artifactId>avaje-logback-encoder</artifactId>
<version>0.5-SNAPSHOT</version>
<version>0.4-SNAPSHOT</version>

<dependencies>
<dependency>
Expand All @@ -26,8 +26,8 @@
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>2.4</version>
<artifactId>avaje-json-core</artifactId>
<version>3.0-RC5</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
Expand Down
43 changes: 23 additions & 20 deletions src/main/java/io/avaje/logback/encoder/JsonbEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import ch.qos.logback.classic.pattern.ThrowableHandlingConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.EncoderBase;
import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.Types;
import io.avaje.jsonb.spi.PropertyNames;
import io.avaje.jsonb.stream.JsonStream;
import io.avaje.json.PropertyNames;
import io.avaje.json.simple.SimpleMapper;
import io.avaje.json.stream.JsonStream;
import io.avaje.logback.encoder.abbreviator.TrimPackageAbbreviator;

public final class JsonbEncoder extends EncoderBase<ILoggingEvent> {
Expand Down Expand Up @@ -48,9 +47,11 @@ public JsonbEncoder() {

@Override
public void start() {
properties = json.properties("@timestamp", "level", "logger", "message", "thread", "stack_trace");
properties =
json.properties("@timestamp", "level", "logger", "message", "thread", "stack_trace");
formatter = TimeZoneUtils.formatter(timestampPattern, timeZone.toZoneId());
fieldExtra = customFieldsMap.entrySet().stream()
fieldExtra =
customFieldsMap.entrySet().stream()
.mapToInt(e -> e.getKey().length() + e.getValue().length())
.sum();
super.start();
Expand Down Expand Up @@ -81,7 +82,8 @@ public byte[] encode(ILoggingEvent event) {
final var threadName = event.getThreadName();
final var message = event.getFormattedMessage();
final var loggerName = event.getLoggerName();
final int bufferSize = 100 + extra + fieldExtra + message.length() + threadName.length() + loggerName.length();
final int bufferSize =
100 + extra + fieldExtra + message.length() + threadName.length() + loggerName.length();
final var outputStream = new ByteArrayOutputStream(bufferSize);

try (var writer = json.writer(outputStream)) {
Expand All @@ -101,14 +103,18 @@ public byte[] encode(ILoggingEvent event) {
writer.name(5);
writer.value(stackTraceBody);
}
customFieldsMap.forEach((k, v) -> {
writer.name(k);
writer.value(v);
});
event.getMDCPropertyMap().forEach((k, v) -> {
writer.name(k);
writer.value(v);
});
customFieldsMap.forEach(
(k, v) -> {
writer.name(k);
writer.value(v);
});
event
.getMDCPropertyMap()
.forEach(
(k, v) -> {
writer.name(k);
writer.value(v);
});
writer.endObject();
writer.writeNewLine();
}
Expand All @@ -120,11 +126,8 @@ public void setCustomFields(String customFields) {
if (customFields == null || customFields.isBlank()) {
return;
}
var jsonb = Jsonb.builder().adapter(json).build();
jsonb
.<Map<String, Object>>type(Types.mapOf(Object.class))
.fromJson(customFields)
.forEach((k, v) -> customFieldsMap.put(k, jsonb.toJson(v)));
var mapper = SimpleMapper.builder().jsonStream(json).build();
mapper.map().fromJson(customFields).forEach((k, v) -> customFieldsMap.put(k, mapper.toJson(v)));
}

public void setTimestampPattern(String pattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* <ul>
* <li>"rootFirst" - indicating that stacks should be printed root-cause first
* <li>"inlineHash" - indicating that hexadecimal error hashes should be computed and inlined
* <li>"inline" - indicating that the whole stack trace should be inlined, using "\\n" as
* <li>"inline" - indicating that the whole stack trace should be inlined, using "\n" as
* separator
* <li>"omitCommonFrames" - omit common frames
* <li>"keepCommonFrames" - keep common frames
Expand Down Expand Up @@ -101,7 +101,7 @@ public final class ShortenedThrowableConverter extends ThrowableHandlingConverte
* String sequence to use to delimit lines instead of {@link CoreConstants#LINE_SEPARATOR} when
* inline is active
*/
public static final String DEFAULT_INLINE_SEPARATOR = "\\n";
public static final String DEFAULT_INLINE_SEPARATOR = "\n";

private final AtomicInteger errorCount = new AtomicInteger();

Expand Down Expand Up @@ -209,7 +209,7 @@ private void parseOptions() {
/*
* Remaining options are either
* - "rootFirst" - indicating that stacks should be printed root-cause first
* - "inline" - indicating that the whole stack trace should be inlined, using "\\n" as separator
* - "inline" - indicating that the whole stack trace should be inlined, using "\n" as separator
* - "omitCommonFrames" - omit common frames
* - "keepCommonFrames" - keep common frames
* - evaluator name - name of evaluators that will determine if the stacktrace is ignored
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

requires transitive ch.qos.logback.classic;
requires transitive ch.qos.logback.core;
requires transitive io.avaje.jsonb;
requires transitive io.avaje.json;
requires transitive org.slf4j;
requires transitive org.slf4j.jdk.platform.logging;
requires java.naming;
Expand Down
Loading