Skip to content

Commit

Permalink
+ bug fix: JsonPattern thread safety problem
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed May 12, 2023
1 parent 860e939 commit 910f682
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.github.elf4j</groupId>
<artifactId>elf4j-engine</artifactId>
<version>8.1.0</version>
<version>8.1.1</version>
<packaging>jar</packaging>
<name>elf4j-engine</name>
<description>A stand-alone Java log engine implementing the ELF4J (Easy Logging Facade for Java) API</description>
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/elf4j/engine/service/pattern/JsonPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import com.dslplatform.json.CompiledJson;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonWriter;
import com.dslplatform.json.PrettifyOutputStream;
import com.dslplatform.json.runtime.Settings;
import elf4j.engine.service.LogEvent;
Expand All @@ -42,6 +41,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Arrays;
Expand All @@ -64,9 +64,8 @@ public class JsonPattern implements LogPattern {
boolean includeCallerThread;
boolean includeCallerDetail;
boolean prettyPrint;
@ToString.Exclude JsonWriter jsonWriter =
new DslJson<>(Settings.withRuntime().skipDefaultValues(true).includeServiceLoader()).newWriter();
@ToString.Exclude ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@ToString.Exclude DslJson<Object> dslJson =
new DslJson<>(Settings.basicSetup().skipDefaultValues(true).includeServiceLoader());

/**
* @param patternSegment
Expand Down Expand Up @@ -105,16 +104,11 @@ public boolean includeCallerThread() {

@Override
public void render(LogEvent logEvent, StringBuilder target) {
jsonWriter.reset();
jsonWriter.serializeObject(JsonLogEntry.from(logEvent, this));
if (!this.prettyPrint) {
target.append(jsonWriter);
return;
}
byteArrayOutputStream.reset();
try (OutputStream outputStream = new PrettifyOutputStream(byteArrayOutputStream)) {
jsonWriter.toStream(outputStream);
target.append(byteArrayOutputStream.toString("UTF-8"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(2048);
try (OutputStream outputStream = this.prettyPrint ? new PrettifyOutputStream(byteArrayOutputStream) :
byteArrayOutputStream) {
dslJson.serialize(JsonLogEntry.from(logEvent, this), outputStream);
target.append(byteArrayOutputStream.toString(StandardCharsets.UTF_8.toString()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down

0 comments on commit 910f682

Please sign in to comment.