From c886112d5b0f47f44eeac526f24e7688a863be01 Mon Sep 17 00:00:00 2001 From: Pascal Schumacher Date: Fri, 23 Oct 2020 11:14:51 +0200 Subject: [PATCH] Close JsonGenerators after usage. --- .../logback/composite/CompositeJsonFormatter.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java b/src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java index 9ec99573..e133860e 100644 --- a/src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java +++ b/src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java @@ -161,8 +161,9 @@ public byte[] writeEventAsBytes(Event event) throws IOException { } public void writeEventToOutputStream(Event event, OutputStream outputStream) throws IOException { - JsonGenerator generator = createGenerator(outputStream); - writeEventToGenerator(generator, event); + try (JsonGenerator generator = createGenerator(outputStream)) { + writeEventToGenerator(generator, event); + } /* * Do not flush the outputStream. * @@ -174,10 +175,11 @@ public void writeEventToOutputStream(Event event, OutputStream outputStream) thr public String writeEventAsString(Event event) throws IOException { SegmentedStringWriter writer = new SegmentedStringWriter(getBufferRecycler()); - JsonGenerator generator = createGenerator(writer); - writeEventToGenerator(generator, event); - writer.flush(); - return writer.getAndClear(); + try (JsonGenerator generator = createGenerator(writer)) { + writeEventToGenerator(generator, event); + writer.flush(); + return writer.getAndClear(); + } } protected void writeEventToGenerator(JsonGenerator generator, Event event) throws IOException {