Use CharSequence instead of 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/85a46bbe Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/85a46bbe Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/85a46bbe Branch: refs/heads/master Commit: 85a46bbe78b749ea14dd2a70cc809932271f5dea Parents: 299f488 Author: Mikael Ståldal <[email protected]> Authored: Fri Apr 15 16:23:11 2016 +0200 Committer: Mikael Ståldal <[email protected]> Committed: Fri Apr 15 16:23:11 2016 +0200 ---------------------------------------------------------------------- .../log4j/message/ReusableSimpleMessage.java | 20 +++++++++--- .../logging/log4j/message/SimpleMessage.java | 27 +++++++++++----- .../log4j/util/CharSequenceFormattable.java | 33 -------------------- .../log4j/core/async/RingBufferLogEvent.java | 21 ++++++++++--- .../logging/log4j/core/layout/GelfLayout.java | 5 ++- 5 files changed, 55 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/85a46bbe/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 2dd46d2..ed6e39e 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 @@ -16,7 +16,6 @@ */ package org.apache.logging.log4j.message; -import org.apache.logging.log4j.util.CharSequenceFormattable; import org.apache.logging.log4j.util.PerformanceSensitive; /** @@ -24,7 +23,7 @@ import org.apache.logging.log4j.util.PerformanceSensitive; * @since 2.6 */ @PerformanceSensitive("allocation") -public class ReusableSimpleMessage implements ReusableMessage, CharSequenceFormattable { +public class ReusableSimpleMessage implements ReusableMessage, CharSequence { private static final long serialVersionUID = -9199974506498249809L; private static Object[] EMPTY_PARAMS = new Object[0]; private CharSequence charSequence; @@ -62,9 +61,22 @@ public class ReusableSimpleMessage implements ReusableMessage, CharSequenceForma buffer.append(charSequence); } + + // CharSequence impl + + @Override + public int length() { + return charSequence == null ? 0 : charSequence.length(); + } + + @Override + public char charAt(int index) { + return charSequence.charAt(index); + } + @Override - public CharSequence getFormattedCharSequence() { - return charSequence; + public CharSequence subSequence(int start, int end) { + return charSequence.subSequence(start, end); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/85a46bbe/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 44dc15e..e26c506 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,7 +16,6 @@ */ package org.apache.logging.log4j.message; -import org.apache.logging.log4j.util.CharSequenceFormattable; import org.apache.logging.log4j.util.StringBuilderFormattable; import java.io.IOException; @@ -26,7 +25,7 @@ import java.io.ObjectOutputStream; /** * The simplest possible implementation of Message. It just returns the String given as the constructor argument. */ -public class SimpleMessage implements Message, StringBuilderFormattable, CharSequenceFormattable { +public class SimpleMessage implements Message, StringBuilderFormattable, CharSequence { private static final long serialVersionUID = -8398002534962715992L; private String message; @@ -74,11 +73,6 @@ public class SimpleMessage implements Message, StringBuilderFormattable, CharSeq buffer.append(charSequence); } - @Override - public CharSequence getFormattedCharSequence() { - return charSequence; - } - /** * Returns the message. * @return the message. @@ -131,6 +125,25 @@ public class SimpleMessage implements Message, StringBuilderFormattable, CharSeq return null; } + + // CharSequence impl + + @Override + public int length() { + return charSequence == null ? 0 : charSequence.length(); + } + + @Override + public char charAt(int index) { + return charSequence.charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return charSequence.subSequence(start, end); + } + + private void writeObject(final ObjectOutputStream out) throws IOException { getFormattedMessage(); // initialize the message:String field out.defaultWriteObject(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/85a46bbe/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 deleted file mode 100644 index b14289d..0000000 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/CharSequenceFormattable.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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/85a46bbe/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 873efc6..9504e8a 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 @@ -30,7 +30,6 @@ import org.apache.logging.log4j.message.Message; 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.CharSequenceFormattable; import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.Strings; @@ -42,7 +41,7 @@ import java.util.Map; * When the Disruptor is started, the RingBuffer is populated with event objects. These objects are then re-used during * the life of the RingBuffer. */ -public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequenceFormattable { +public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequence { /** The {@code EventFactory} for {@code RingBufferLogEvent}s. */ public static final Factory FACTORY = new Factory(); @@ -242,11 +241,25 @@ public class RingBufferLogEvent implements LogEvent, ReusableMessage, CharSequen buffer.append(messageText); } + + // CharSequence impl + @Override - public CharSequence getFormattedCharSequence() { - return messageText; + public int length() { + return messageText.length(); + } + + @Override + public char charAt(int index) { + return messageText.charAt(index); } + @Override + public CharSequence subSequence(int start, int end) { + return messageText.subSequence(start, end); + } + + private Message getNonNullImmutableMessage() { return message != null ? message : new SimpleMessage(String.valueOf(messageText)); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/85a46bbe/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 1854004..24ab6c4 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 @@ -29,7 +29,6 @@ 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; @@ -221,8 +220,8 @@ public final class GelfLayout extends AbstractStringLayout { builder.append("\"short_message\":\""); Message message = event.getMessage(); - if (message instanceof CharSequenceFormattable) { - JsonUtils.quoteAsString(toNullSafeString(((CharSequenceFormattable)message).getFormattedCharSequence()), builder); + if (message instanceof CharSequence) { + JsonUtils.quoteAsString(((CharSequence)message), builder); } else if (gcFree && message instanceof StringBuilderFormattable) { StringBuilder messageBuffer = getMessageStringBuilder(); ((StringBuilderFormattable)message).formatTo(messageBuffer);
