Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-1356 7c990c4a0 -> a91a54b95
LOG4J2-1291 replace ThreadLocal<TextEncoderHelper> in AbstractStringLayout with plain field: the ByteBufferDestination needs to be protected from concurrent access anyway Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/19bafa4c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/19bafa4c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/19bafa4c Branch: refs/heads/LOG4J2-1356 Commit: 19bafa4c8b24d07fa90cd5dc190c365ca38667f8 Parents: 4d1d60d Author: rpopma <[email protected]> Authored: Fri Apr 8 19:46:48 2016 +0900 Committer: rpopma <[email protected]> Committed: Fri Apr 8 19:46:48 2016 +0900 ---------------------------------------------------------------------- .../log4j/core/layout/AbstractStringLayout.java | 44 ++++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/19bafa4c/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java index 84dbd69..19dbaf4 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/AbstractStringLayout.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.StringLayout; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.core.util.StringEncoder; import org.apache.logging.log4j.util.Strings; @@ -58,7 +59,7 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem private static final ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<>(); - private final ThreadLocal<TextEncoderHelper> textEncoderHelper = new ThreadLocal<>(); + private final TextEncoderHelper textEncoderHelper; /** * Returns a {@code StringBuilder} that this Layout implementation can use to write the formatted log event to. @@ -75,20 +76,6 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem return result; } - /** - * Returns a {@code TextEncoderHelper} that this Layout implementation can use for encoding log events. - * - * @return a {@code TextEncoderHelper} - */ - protected TextEncoderHelper getCachedTextEncoderHelper() { - TextEncoderHelper result = textEncoderHelper.get(); - if (result == null) { - result = new TextEncoderHelper(getCharset()); - textEncoderHelper.set(result); - } - return result; - } - // LOG4J2-1151: If the built-in JDK 8 encoders are available we should use them. private static boolean isPreJava8() { final String version = System.getProperty("java.version"); @@ -120,37 +107,48 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem /** * Builds a new layout. - * @param charset the charset used to encode the header bytes, footer bytes and anything else that needs to be + * @param aCharset the charset used to encode the header bytes, footer bytes and anything else that needs to be * converted from strings to bytes. * @param header the header bytes * @param footer the footer bytes */ - protected AbstractStringLayout(final Charset charset, final byte[] header, final byte[] footer) { + protected AbstractStringLayout(final Charset aCharset, final byte[] header, final byte[] footer) { super(null, header, footer); this.headerSerializer = null; this.footerSerializer = null; - this.charset = charset == null ? StandardCharsets.UTF_8 : charset; + this.charset = aCharset == null ? StandardCharsets.UTF_8 : aCharset; this.charsetName = this.charset.name(); useCustomEncoding = isPreJava8() - && (StandardCharsets.ISO_8859_1.equals(charset) || StandardCharsets.US_ASCII.equals(charset)); + && (StandardCharsets.ISO_8859_1.equals(aCharset) || StandardCharsets.US_ASCII.equals(aCharset)); + textEncoderHelper = Constants.ENABLE_DIRECT_ENCODERS ? new TextEncoderHelper(charset) : null; } /** * Builds a new layout. * @param config the configuration - * @param charset the charset used to encode the header bytes, footer bytes and anything else that needs to be + * @param aCharset the charset used to encode the header bytes, footer bytes and anything else that needs to be * converted from strings to bytes. * @param headerSerializer the header bytes serializer * @param footerSerializer the footer bytes serializer */ - protected AbstractStringLayout(final Configuration config, final Charset charset, final Serializer headerSerializer, final Serializer footerSerializer) { + protected AbstractStringLayout(final Configuration config, final Charset aCharset, final Serializer headerSerializer, final Serializer footerSerializer) { super(config, null, null); this.headerSerializer = headerSerializer; this.footerSerializer = footerSerializer; - this.charset = charset == null ? StandardCharsets.UTF_8 : charset; + this.charset = aCharset == null ? StandardCharsets.UTF_8 : aCharset; this.charsetName = this.charset.name(); useCustomEncoding = isPreJava8() - && (StandardCharsets.ISO_8859_1.equals(charset) || StandardCharsets.US_ASCII.equals(charset)); + && (StandardCharsets.ISO_8859_1.equals(aCharset) || StandardCharsets.US_ASCII.equals(aCharset)); + textEncoderHelper = Constants.ENABLE_DIRECT_ENCODERS ? new TextEncoderHelper(charset) : null; + } + + /** + * Returns a {@code TextEncoderHelper} that this Layout implementation can use for encoding log events. + * + * @return a {@code TextEncoderHelper} + */ + protected TextEncoderHelper getCachedTextEncoderHelper() { + return textEncoderHelper; } protected byte[] getBytes(final String s) {
