logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject [01/12] logging-log4j2 git commit: CharSequenceFormattable
Date Mon, 18 Apr 2016 11:44:16 GMT
Repository: logging-log4j2
Updated Branches:
  refs/heads/master d3e571f37 -> 86934ca41


CharSequenceFormattable


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

Branch: refs/heads/master
Commit: 07723d5cf7078300b63bae462f5557d11705d3fe
Parents: 90adca7
Author: Mikael Ståldal <mikael.staldal@magine.com>
Authored: Wed Apr 13 17:53:40 2016 +0200
Committer: Mikael Ståldal <mikael.staldal@magine.com>
Committed: Wed Apr 13 17:53:40 2016 +0200

----------------------------------------------------------------------
 .../logging/log4j/message/ObjectMessage.java    | 16 ++++++++--
 .../log4j/message/ParameterizedMessage.java     | 18 +++++++++--
 .../logging/log4j/message/ReusableMessage.java  |  3 +-
 .../log4j/message/ReusableObjectMessage.java    |  9 ++++++
 .../message/ReusableParameterizedMessage.java   | 10 ++++--
 .../log4j/message/ReusableSimpleMessage.java    |  5 +++
 .../logging/log4j/message/SimpleMessage.java    | 13 +++++---
 .../log4j/message/ThreadDumpMessage.java        | 19 ++++++++---
 .../log4j/util/CharSequenceFormattable.java     | 33 ++++++++++++++++++++
 .../log4j/core/async/RingBufferLogEvent.java    | 14 ++++++---
 .../logging/log4j/core/layout/GelfLayout.java   |  6 ++--
 11 files changed, 122 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ObjectMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ObjectMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/ObjectMessage.java
index db8c31b..11e2731 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ObjectMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ObjectMessage.java
@@ -16,17 +16,18 @@
  */
 package org.apache.logging.log4j.message;
 
+import org.apache.logging.log4j.util.CharSequenceFormattable;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 
-import org.apache.logging.log4j.util.StringBuilderFormattable;
-
 /**
  * Handles messages that contain an Object.
  */
-public class ObjectMessage implements Message, StringBuilderFormattable {
+public class ObjectMessage implements Message, StringBuilderFormattable, CharSequenceFormattable
{
 
     private static final long serialVersionUID = -5903272448334166185L;
 
@@ -69,6 +70,15 @@ public class ObjectMessage implements Message, StringBuilderFormattable
{
         }
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        if (obj instanceof CharSequence) {
+            return (CharSequence) obj;
+        } else {
+            return getFormattedMessage();
+        }
+    }
+
     /**
      * Returns the object formatted using its toString method.
      *

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java
index 6c5b8e7..4e8648e 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterizedMessage.java
@@ -16,10 +16,11 @@
  */
 package org.apache.logging.log4j.message;
 
-import java.util.Arrays;
-
+import org.apache.logging.log4j.util.CharSequenceFormattable;
 import org.apache.logging.log4j.util.StringBuilderFormattable;
 
+import java.util.Arrays;
+
 /**
  * Handles messages that consist of a format string containing '{}' to represent each replaceable
token, and
  * the parameters.
@@ -28,7 +29,7 @@ import org.apache.logging.log4j.util.StringBuilderFormattable;
  * licensed under the LGPL. It has been relicensed here with his permission providing that
this attribution remain.
  * </p>
  */
-public class ParameterizedMessage implements Message, StringBuilderFormattable {
+public class ParameterizedMessage implements Message, StringBuilderFormattable, CharSequenceFormattable
{
     /**
      * Prefix for recursion.
      */
@@ -219,6 +220,17 @@ public class ParameterizedMessage implements Message, StringBuilderFormattable
{
         }
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        if (formattedMessage != null) {
+            return formattedMessage;
+        } else {
+            final StringBuilder buffer = getThreadLocalStringBuilder();
+            formatTo(buffer);
+            return buffer;
+        }
+    }
+
     /**
      * Replace placeholders in the given messagePattern with arguments.
      *

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableMessage.java
index a6da9c4..ad1fc6d 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableMessage.java
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.message;
 
+import org.apache.logging.log4j.util.CharSequenceFormattable;
 import org.apache.logging.log4j.util.PerformanceSensitive;
 import org.apache.logging.log4j.util.StringBuilderFormattable;
 
@@ -29,5 +30,5 @@ import org.apache.logging.log4j.util.StringBuilderFormattable;
  * @since 2.6
  */
 @PerformanceSensitive("allocation")
-public interface ReusableMessage extends Message, StringBuilderFormattable {
+public interface ReusableMessage extends Message, StringBuilderFormattable, CharSequenceFormattable
{
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableObjectMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableObjectMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableObjectMessage.java
index 12f62f4..d40bb00 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableObjectMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableObjectMessage.java
@@ -57,6 +57,15 @@ public class ReusableObjectMessage implements ReusableMessage {
         }
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        if (obj instanceof CharSequence) {
+            return (CharSequence) obj;
+        } else {
+            return getFormattedMessage();
+        }
+    }
+
     /**
      * Returns the object formatted using its toString method.
      *

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
index 0c099cf..bd084e0 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
@@ -16,10 +16,10 @@
  */
 package org.apache.logging.log4j.message;
 
-import java.util.Arrays;
-
 import org.apache.logging.log4j.util.PerformanceSensitive;
 
+import java.util.Arrays;
+
 /**
  * Reusable parameterized message. This message is mutable and is not safe to be accessed
or modified by multiple
  * threads concurrently.
@@ -257,6 +257,12 @@ public class ReusableParameterizedMessage implements ReusableMessage
{
         }
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        final StringBuilder sb = getBuffer();
+        formatTo(sb);
+        return sb;
+    }
 
     @Override
     public String toString() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableSimpleMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableSimpleMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableSimpleMessage.java
index a0954dc..68bf7ba 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableSimpleMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableSimpleMessage.java
@@ -60,5 +60,10 @@ public class ReusableSimpleMessage implements ReusableMessage {
     public void formatTo(final StringBuilder buffer) {
         buffer.append(charSequence);
     }
+
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        return charSequence;
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/SimpleMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/SimpleMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/SimpleMessage.java
index 31d35b1..44dc15e 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/SimpleMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/SimpleMessage.java
@@ -16,17 +16,17 @@
  */
 package org.apache.logging.log4j.message;
 
+import org.apache.logging.log4j.util.CharSequenceFormattable;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.Serializable;
-
-import org.apache.logging.log4j.util.StringBuilderFormattable;
 
 /**
  * The simplest possible implementation of Message. It just returns the String given as the
constructor argument.
  */
-public class SimpleMessage implements Message, StringBuilderFormattable {
+public class SimpleMessage implements Message, StringBuilderFormattable, CharSequenceFormattable
{
     private static final long serialVersionUID = -8398002534962715992L;
 
     private String message;
@@ -74,6 +74,11 @@ public class SimpleMessage implements Message, StringBuilderFormattable
{
         buffer.append(charSequence);
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        return charSequence;
+    }
+
     /**
      * Returns the message.
      * @return the message.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
index 67f0d5f..2a9b193 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
@@ -16,6 +16,10 @@
  */
 package org.apache.logging.log4j.message;
 
+import org.apache.logging.log4j.util.CharSequenceFormattable;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
+import org.apache.logging.log4j.util.Strings;
+
 import java.io.InvalidObjectException;
 import java.io.ObjectInputStream;
 import java.io.Serializable;
@@ -26,13 +30,10 @@ import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.logging.log4j.util.StringBuilderFormattable;
-import org.apache.logging.log4j.util.Strings;
-
 /**
  * Captures information about all running Threads.
  */
-public class ThreadDumpMessage implements Message, StringBuilderFormattable {
+public class ThreadDumpMessage implements Message, StringBuilderFormattable, CharSequenceFormattable
{
 
     private static final long serialVersionUID = -1103400781608841088L;
 
@@ -103,6 +104,16 @@ public class ThreadDumpMessage implements Message, StringBuilderFormattable
{
         }
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        if (formattedMessage != null) {
+            return formattedMessage;
+        }
+        final StringBuilder sb = new StringBuilder(255);
+        formatTo(sb);
+        return sb;
+    }
+
     /**
      * Returns the title.
      * @return the title.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/log4j-api/src/main/java/org/apache/logging/log4j/util/CharSequenceFormattable.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/CharSequenceFormattable.java
b/log4j-api/src/main/java/org/apache/logging/log4j/util/CharSequenceFormattable.java
new file mode 100644
index 0000000..b14289d
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/CharSequenceFormattable.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.util;
+
+/**
+ * Objects that implement this interface can be converted to text, ideally without allocating
temporary objects.
+ *
+ * @since 2.6
+ */
+public interface CharSequenceFormattable {
+
+    /**
+     * Return a text representation of this object, ideally without allocating temporary
objects or copying strings.
+     *
+     * This may return a shared buffer (such as a {@link StringBuilder}). The caller should
quickly consume
+     * the data and not keep any reference to the returned {@link CharSequence}.
+     */
+    CharSequence getFormattedCharSequence();
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/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 f3ad22f..2b79e1b 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
@@ -16,10 +16,7 @@
  */
 package org.apache.logging.log4j.core.async;
 
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
+import com.lmax.disruptor.EventFactory;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.ThreadContext.ContextStack;
@@ -36,7 +33,9 @@ import org.apache.logging.log4j.message.TimestampMessage;
 import org.apache.logging.log4j.util.PropertiesUtil;
 import org.apache.logging.log4j.util.Strings;
 
-import com.lmax.disruptor.EventFactory;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * When the Disruptor is started, the RingBuffer is populated with event objects. These objects
are then re-used during
@@ -242,6 +241,11 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage
{
         buffer.append(messageText);
     }
 
+    @Override
+    public CharSequence getFormattedCharSequence() {
+        return messageText;
+    }
+
     private Message getNonNullImmutableMessage() {
         return message != null ? message : new SimpleMessage(String.valueOf(messageText));
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/07723d5c/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 cef4e46..9ee522b 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
@@ -25,11 +25,11 @@ import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
 import org.apache.logging.log4j.core.net.Severity;
-import org.apache.logging.log4j.core.util.Constants;
 import org.apache.logging.log4j.core.util.JsonUtils;
 import org.apache.logging.log4j.core.util.KeyValuePair;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.CharSequenceFormattable;
 import org.apache.logging.log4j.util.StringBuilderFormattable;
 import org.apache.logging.log4j.util.Strings;
 
@@ -217,7 +217,9 @@ public final class GelfLayout extends AbstractStringLayout {
 
         builder.append("\"short_message\":\"");
         Message message = event.getMessage();
-        if (gcFree && message instanceof StringBuilderFormattable) {
+        if (message instanceof CharSequenceFormattable) {
+            JsonUtils.quoteAsString(toNullSafeString(((CharSequenceFormattable)message).getFormattedCharSequence()),
builder);
+        } else if (gcFree && message instanceof StringBuilderFormattable) {
             StringBuilder messageBuffer = getMessageStringBuilder();
             ((StringBuilderFormattable)message).formatTo(messageBuffer);
             JsonUtils.quoteAsString(messageBuffer, builder);


Mime
View raw message