LOG4J2-1434 Ensure that the thread-local StringBuilder in GelfLayout will not retain excessive memory after a large message was logged.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/aed2298e Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/aed2298e Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/aed2298e Branch: refs/heads/LOG4J-1181 Commit: aed2298e3272164804266a7ed7a47b8b6dc3ee00 Parents: 13b453c Author: Mikael Ståldal <mikael.stal...@magine.com> Authored: Sun Jun 19 11:08:06 2016 +0200 Committer: Mikael Ståldal <mikael.stal...@magine.com> Committed: Sun Jun 19 11:08:06 2016 +0200 ---------------------------------------------------------------------- .../logging/log4j/core/layout/GelfLayout.java | 37 +++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aed2298e/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java index 13a14f9..098d18c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/GelfLayout.java @@ -16,6 +16,17 @@ */ package org.apache.logging.log4j.core.layout; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Map; +import java.util.zip.DeflaterOutputStream; +import java.util.zip.GZIPOutputStream; + import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.Layout; import org.apache.logging.log4j.core.LogEvent; @@ -32,17 +43,6 @@ import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.StringBuilderFormattable; import org.apache.logging.log4j.util.Strings; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.Map; -import java.util.zip.DeflaterOutputStream; -import java.util.zip.GZIPOutputStream; - /** * Lays out events in the Graylog Extended Log Format (GELF) 1.1. * <p> @@ -231,8 +231,12 @@ public final class GelfLayout extends AbstractStringLayout { JsonUtils.quoteAsString(((CharSequence)message), builder); } else if (gcFree && message instanceof StringBuilderFormattable) { final StringBuilder messageBuffer = getMessageStringBuilder(); - ((StringBuilderFormattable)message).formatTo(messageBuffer); - JsonUtils.quoteAsString(messageBuffer, builder); + try { + ((StringBuilderFormattable) message).formatTo(messageBuffer); + JsonUtils.quoteAsString(messageBuffer, builder); + } finally { + returnMessageStringBuilder(messageBuffer); + } } else { JsonUtils.quoteAsString(toNullSafeString(message.getFormattedMessage()), builder); } @@ -253,6 +257,13 @@ public final class GelfLayout extends AbstractStringLayout { return result; } + private void returnMessageStringBuilder(StringBuilder stringBuilder) { + if (stringBuilder.length() > MAX_STRING_BUILDER_SIZE) { + stringBuilder.setLength(MAX_STRING_BUILDER_SIZE); + stringBuilder.trimToSize(); + } + } + private CharSequence toNullSafeString(final CharSequence s) { return s == null ? Strings.EMPTY : s; }