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 2016/11/08 15:03:40 UTC

logging-log4j2 git commit: LOG4J2-1684 reduce allocation of temporary objects in StructuredDataMessage

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 56ae5a8f8 -> 0413ea68f


LOG4J2-1684 reduce allocation of temporary objects in StructuredDataMessage


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

Branch: refs/heads/master
Commit: 0413ea68fa4db2167835a51c9177f9a70846323f
Parents: 56ae5a8
Author: rpopma <rp...@apache.org>
Authored: Wed Nov 9 00:03:35 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Nov 9 00:03:35 2016 +0900

----------------------------------------------------------------------
 .../logging/log4j/message/StructuredDataId.java | 30 ++++++++++++------
 .../log4j/message/StructuredDataMessage.java    | 32 ++++++++++++++++----
 2 files changed, 47 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0413ea68/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java
index 4bc841f..6b5fa99 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataId.java
@@ -18,12 +18,13 @@ package org.apache.logging.log4j.message;
 
 import java.io.Serializable;
 
+import org.apache.logging.log4j.util.StringBuilderFormattable;
 import org.apache.logging.log4j.util.Strings;
 
 /**
  * The StructuredData identifier.
  */
-public class StructuredDataId implements Serializable {
+public class StructuredDataId implements Serializable, StringBuilderFormattable {
 
     /**
      * RFC 5424 Time Quality.
@@ -109,7 +110,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Creates an id using another id to supply default values.
-     * 
+     *
      * @param id The original StructuredDataId.
      * @return the new StructuredDataId.
      */
@@ -122,7 +123,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Creates an id based on the current id.
-     * 
+     *
      * @param defaultId The default id to use if this StructuredDataId doesn't have a name.
      * @param anEnterpriseNumber The enterprise number.
      * @return a StructuredDataId.
@@ -149,7 +150,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Returns a list of required keys.
-     * 
+     *
      * @return a List of required keys or null if none have been provided.
      */
     public String[] getRequired() {
@@ -158,7 +159,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Returns a list of optional keys.
-     * 
+     *
      * @return a List of optional keys or null if none have been provided.
      */
     public String[] getOptional() {
@@ -167,7 +168,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Returns the StructuredDataId name.
-     * 
+     *
      * @return the StructuredDataId name.
      */
     public String getName() {
@@ -176,7 +177,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Returns the enterprise number.
-     * 
+     *
      * @return the enterprise number.
      */
     public int getEnterpriseNumber() {
@@ -185,7 +186,7 @@ public class StructuredDataId implements Serializable {
 
     /**
      * Indicates if the id is reserved.
-     * 
+     *
      * @return true if the id uses the reserved enterprise number, false otherwise.
      */
     public boolean isReserved() {
@@ -194,6 +195,17 @@ public class StructuredDataId implements Serializable {
 
     @Override
     public String toString() {
-        return isReserved() ? name : name + AT_SIGN + enterpriseNumber;
+        final StringBuilder sb = new StringBuilder(name.length() + 10);
+        formatTo(sb);
+        return sb.toString();
+    }
+
+    @Override
+    public void formatTo(final StringBuilder buffer) {
+        if (isReserved()) {
+            buffer.append(name);
+        } else {
+            buffer.append(name).append(AT_SIGN).append(enterpriseNumber);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0413ea68/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
index f29732e..d7f485a 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java
@@ -20,6 +20,8 @@ package org.apache.logging.log4j.message;
 import java.util.Map;
 
 import org.apache.logging.log4j.util.EnglishEnums;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+import org.apache.logging.log4j.util.StringBuilders;
 
 /**
  * Represents a Message that conforms to an RFC 5424 StructuredData element along with the syslog message.
@@ -32,7 +34,7 @@ import org.apache.logging.log4j.util.EnglishEnums;
  *
  * @see <a href="https://tools.ietf.org/html/rfc5424">RFC 5424</a>
  */
-public class StructuredDataMessage extends MapMessage {
+public class StructuredDataMessage extends MapMessage implements StringBuilderFormattable {
 
     private static final long serialVersionUID = 1703221292892071920L;
     private static final int MAX_LENGTH = 32;
@@ -195,6 +197,11 @@ public class StructuredDataMessage extends MapMessage {
         this.type = type;
     }
 
+    @Override
+    public void formatTo(final StringBuilder buffer) {
+        asString(Format.FULL, null, buffer);
+    }
+
     /**
      * Returns the message.
      * @return the message.
@@ -264,25 +271,39 @@ public class StructuredDataMessage extends MapMessage {
      */
     public final String asString(final Format format, final StructuredDataId structuredDataId) {
         final StringBuilder sb = new StringBuilder();
+        asString(format, structuredDataId, sb);
+        return sb.toString();
+    }
+
+    /**
+     * Formats the structured data as described in RFC 5424.
+     *
+     * @param format           "full" will include the type and message. null will return only the STRUCTURED-DATA as
+     *                         described in RFC 5424
+     * @param structuredDataId The SD-ID as described in RFC 5424. If null the value in the StructuredData
+     *                         will be used.
+     * @param sb The StringBuilder to append the formatted message to.
+     */
+    public final void asString(final Format format, final StructuredDataId structuredDataId, final StringBuilder sb) {
         final boolean full = Format.FULL.equals(format);
         if (full) {
             final String myType = getType();
             if (myType == null) {
-                return sb.toString();
+                return;
             }
             sb.append(getType()).append(' ');
         }
         StructuredDataId sdId = getId();
         if (sdId != null) {
-            sdId = sdId.makeId(structuredDataId);
+            sdId = sdId.makeId(structuredDataId); // TODO can we avoid allocating this object?
         } else {
             sdId = structuredDataId;
         }
         if (sdId == null || sdId.getName() == null) {
-            return sb.toString();
+            return;
         }
         sb.append('[');
-        sb.append(sdId);
+        StringBuilders.appendValue(sb, sdId); // avoids toString if implements StringBuilderFormattable
         sb.append(' ');
         appendMap(sb);
         sb.append(']');
@@ -292,7 +313,6 @@ public class StructuredDataMessage extends MapMessage {
                 sb.append(' ').append(msg);
             }
         }
-        return sb.toString();
     }
 
     /**