You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2017/04/16 07:56:47 UTC

[8/9] logging-log4j2 git commit: LOG4J2-1858 refactor existing places that trim a StringBuilder to max size to use the StringBuilders utility method

LOG4J2-1858 refactor existing places that trim a StringBuilder to max size to use the StringBuilders utility method


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fd331b21
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fd331b21
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fd331b21

Branch: refs/heads/master
Commit: fd331b217536ef354e72922d14b26c9694e8c38c
Parents: 0f70fe7
Author: rpopma <rp...@apache.org>
Authored: Sun Apr 16 16:53:10 2017 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sun Apr 16 16:53:10 2017 +0900

----------------------------------------------------------------------
 .../log4j/core/async/RingBufferLogEvent.java    | 22 +++++++-------------
 .../log4j/core/filter/StructuredDataFilter.java |  5 +----
 .../log4j/core/impl/MutableLogEvent.java        | 13 ++++--------
 .../log4j/core/layout/AbstractStringLayout.java | 12 +++++------
 .../log4j/core/pattern/MdcPatternConverter.java |  6 +-----
 5 files changed, 19 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd331b21/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
index 6766f21..abfd392 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java
@@ -23,19 +23,20 @@ import java.util.Map;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.ThreadContext.ContextStack;
-import org.apache.logging.log4j.message.AsynchronouslyFormattable;
-import org.apache.logging.log4j.util.ReadOnlyStringMap;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.ContextDataFactory;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
-import org.apache.logging.log4j.util.StringMap;
 import org.apache.logging.log4j.core.impl.ThrowableProxy;
 import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.message.AsynchronouslyFormattable;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.message.ReusableMessage;
 import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.message.TimestampMessage;
+import org.apache.logging.log4j.util.ReadOnlyStringMap;
+import org.apache.logging.log4j.util.StringBuilders;
+import org.apache.logging.log4j.util.StringMap;
 import org.apache.logging.log4j.util.Strings;
 
 import com.lmax.disruptor.EventFactory;
@@ -118,7 +119,7 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen
     public LogEvent toImmutable() {
         return createMemento();
     }
-    
+
     private void setMessage(final Message msg) {
         if (msg instanceof ReusableMessage) {
             final ReusableMessage reusable = (ReusableMessage) msg;
@@ -406,7 +407,8 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen
             }
         }
 
-        trimMessageText();
+        // ensure that excessively long char[] arrays are not kept in memory forever
+        StringBuilders.trimToMaxSize(messageText, Constants.MAX_REUSABLE_MESSAGE_SIZE);
 
         if (parameters != null) {
             for (int i = 0; i < parameters.length; i++) {
@@ -415,14 +417,6 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen
         }
     }
 
-    // ensure that excessively long char[] arrays are not kept in memory forever
-    private void trimMessageText() {
-        if (messageText != null && messageText.length() > Constants.MAX_REUSABLE_MESSAGE_SIZE) {
-            messageText.setLength(Constants.MAX_REUSABLE_MESSAGE_SIZE);
-            messageText.trimToSize();
-        }
-    }
-
     private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
         getThrownProxy(); // initialize the ThrowableProxy before serializing
         out.defaultWriteObject();
@@ -435,7 +429,7 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen
      */
     public LogEvent createMemento() {
         return new Log4jLogEvent.Builder(this).build();
-        
+
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd331b21/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
index 111b1cd..c759103 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StructuredDataFilter.java
@@ -111,10 +111,7 @@ public final class StructuredDataFilter extends MapFilter {
             result = new StringBuilder();
             threadLocalStringBuilder.set(result);
         }
-        if (result.length() > MAX_BUFFER_SIZE) {
-            result.setLength(MAX_BUFFER_SIZE);
-            result.trimToSize();
-        }
+        StringBuilders.trimToMaxSize(result, MAX_BUFFER_SIZE);
         result.setLength(0);
         return result;
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd331b21/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
index 3b96f7a..3b13862 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java
@@ -32,6 +32,7 @@ import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.message.ReusableMessage;
 import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.util.StringBuilders;
 import org.apache.logging.log4j.util.StringMap;
 import org.apache.logging.log4j.util.Strings;
 
@@ -137,7 +138,9 @@ public class MutableLogEvent implements LogEvent, ReusableMessage {
         // where this instance is kept in a ThreadLocal, so it usually does not change.
         // threadName = null; // no need to clear threadName
 
-        trimMessageText();
+        // ensure that excessively long char[] arrays are not kept in memory forever
+        StringBuilders.trimToMaxSize(messageText, Constants.MAX_REUSABLE_MESSAGE_SIZE);
+
         if (parameters != null) {
             for (int i = 0; i < parameters.length; i++) {
                 parameters[i] = null;
@@ -153,14 +156,6 @@ public class MutableLogEvent implements LogEvent, ReusableMessage {
         //nanoTime;
     }
 
-    // ensure that excessively long char[] arrays are not kept in memory forever
-    private void trimMessageText() {
-        if (messageText != null && messageText.length() > Constants.MAX_REUSABLE_MESSAGE_SIZE) {
-            messageText.setLength(Constants.MAX_REUSABLE_MESSAGE_SIZE);
-            messageText.trimToSize();
-        }
-    }
-
     @Override
     public String getLoggerFqcn() {
         return loggerFqcn;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd331b21/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 f726fce..14b3d0d 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
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
 import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.core.util.StringEncoder;
 import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.StringBuilders;
 import org.apache.logging.log4j.util.Strings;
 
 /**
@@ -46,7 +47,7 @@ import org.apache.logging.log4j.util.Strings;
 public abstract class AbstractStringLayout extends AbstractLayout<String> implements StringLayout {
 
     public abstract static class Builder<B extends Builder<B>> extends AbstractLayout.Builder<B> {
-        
+
         @PluginBuilderAttribute(value = "charset")
         private Charset charset;
 
@@ -82,9 +83,9 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem
             this.headerSerializer = headerSerializer;
             return asBuilder();
         }
-        
+
     }
-    
+
     public interface Serializer {
         String toSerializable(final LogEvent event);
     }
@@ -140,10 +141,7 @@ public abstract class AbstractStringLayout extends AbstractLayout<String> implem
     }
 
     protected static void trimToMaxSize(final StringBuilder stringBuilder) {
-        if (stringBuilder.length() > MAX_STRING_BUILDER_SIZE) {
-            stringBuilder.setLength(MAX_STRING_BUILDER_SIZE);
-            stringBuilder.trimToSize();
-        }
+        StringBuilders.trimToMaxSize(stringBuilder, MAX_STRING_BUILDER_SIZE);
     }
 
     private Encoder<StringBuilder> textEncoder;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd331b21/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
index 7e99770..1159554 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MdcPatternConverter.java
@@ -38,7 +38,6 @@ public final class MdcPatternConverter extends LogEventPatternConverter {
 
     private static final ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<>();
     private static final int DEFAULT_STRING_BUILDER_SIZE = 64;
-    private static final int MAX_STRING_BUILDER_SIZE = Constants.MAX_REUSABLE_MESSAGE_SIZE;
 
     /**
      * Name of property to output.
@@ -165,9 +164,6 @@ public final class MdcPatternConverter extends LogEventPatternConverter {
     }
 
     private static void trimToMaxSize(final StringBuilder stringBuilder) {
-        if (stringBuilder.length() > MAX_STRING_BUILDER_SIZE) {
-            stringBuilder.setLength(MAX_STRING_BUILDER_SIZE);
-            stringBuilder.trimToSize();
-        }
+        StringBuilders.trimToMaxSize(stringBuilder, Constants.MAX_REUSABLE_MESSAGE_SIZE);
     }
 }