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/04/05 17:53:03 UTC

[11/23] logging-log4j2 git commit: LOG4J2-1344 added method ByteBufferDestination.size() - count total bytes written to destination

LOG4J2-1344 added method ByteBufferDestination.size() - count total bytes written to destination

The size() method is necessary to support file rollover (file size trigger):
Managers for Rolling...Appenders currently override the write() method and count the number of written bytes there.
When the Layout encodes the LogEvent directly into the ByteBufferDestination, the write() method is not called.
The ByteBufferDestination.size() gives the Manager enough information to decide if a rollover is required.


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

Branch: refs/heads/LOG4J2-1343-no-gc-outputstreamappenders
Commit: 6aea61e098bec36a31908f6b12445516229569d5
Parents: 3a871da
Author: rpopma <rp...@apache.org>
Authored: Tue Apr 5 22:53:45 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Apr 5 22:53:45 2016 +0900

----------------------------------------------------------------------
 .../logging/log4j/core/layout/ByteBufferDestination.java     | 8 ++++++++
 .../log4j/core/util/ByteBufferDestinationOutputStream.java   | 7 +++++++
 .../apache/logging/log4j/core/layout/PatternLayoutTest.java  | 7 ++++++-
 .../logging/log4j/core/layout/SpyByteBufferDestination.java  | 7 +++++++
 .../logging/log4j/test/appender/EncodingListAppender.java    | 5 +++++
 .../logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java   | 5 +++++
 .../org/apache/logging/log4j/perf/nogc/DemoAppender.java     | 7 +++++++
 7 files changed, 45 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ByteBufferDestination.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ByteBufferDestination.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ByteBufferDestination.java
index 61ce3ba..65103c6 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ByteBufferDestination.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/ByteBufferDestination.java
@@ -42,4 +42,12 @@ public interface ByteBufferDestination {
      * @return a buffer with more available space (which may or may not be the same instance)
      */
     ByteBuffer drain(ByteBuffer buf);
+
+    /**
+     * Returns the number of bytes that have been written to this destination.
+     *
+     * @return the number of bytes that have been {@link #drain(ByteBuffer) drained} plus the number of bytes that are
+     *          still in the buffer.
+     */
+    long size();
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
index b6a7ed4..f832df4 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
@@ -37,6 +37,7 @@ import org.apache.logging.log4j.core.layout.ByteBufferDestination;
 public class ByteBufferDestinationOutputStream extends FilterOutputStream implements ByteBufferDestination {
     private static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
     private final ByteBuffer byteBuffer;
+    private long drained;
 
     public ByteBufferDestinationOutputStream(final OutputStream out) {
         this(out, DEFAULT_BUFFER_SIZE);
@@ -64,6 +65,7 @@ public class ByteBufferDestinationOutputStream extends FilterOutputStream implem
         buf.flip();
         try {
             out.write(buf.array(), 0, buf.limit());
+            drained += buf.limit();
         } catch (final IOException ex) {
             throw new IllegalStateException("Could not write " + buf.limit() + " bytes to " + out, ex);
         }
@@ -71,6 +73,11 @@ public class ByteBufferDestinationOutputStream extends FilterOutputStream implem
         return buf;
     }
 
+    @Override
+    public long size() {
+        return drained + byteBuffer.position();
+    }
+
     /**
      * Writes any data remaining in the {@code ByteBuffer} to the {@code OutputStream}.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
index 988b42b..5678721 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
@@ -86,6 +86,11 @@ public class PatternLayoutTest {
         public ByteBuffer drain(final ByteBuffer buf) {
             throw new IllegalStateException("Unexpected message larger than 2048 bytes");
         }
+
+        @Override
+        public long size() {
+            return byteBuffer.position();
+        }
     }
 
     private void assertToByteArray(final String expectedStr, final PatternLayout layout, final LogEvent event) {
@@ -239,7 +244,7 @@ public class PatternLayoutTest {
                 .setIncludeLocation(true)
                 .setMessage(new SimpleMessage("entry")).build();
         final String result1 = new FauxLogger().formatEvent(event1, layout);
-        final String expectSuffix1 = String.format("====== PatternLayoutTest.testPatternSelector:241 entry ======%n");
+        final String expectSuffix1 = String.format("====== PatternLayoutTest.testPatternSelector:246 entry ======%n");
         assertTrue("Unexpected result: " + result1, result1.endsWith(expectSuffix1));
         final LogEvent event2 = Log4jLogEvent.newBuilder() //
                 .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") //

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/SpyByteBufferDestination.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/SpyByteBufferDestination.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/SpyByteBufferDestination.java
index 5d8f3b5..34b898d 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/SpyByteBufferDestination.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/SpyByteBufferDestination.java
@@ -27,6 +27,7 @@ public class SpyByteBufferDestination implements ByteBufferDestination {
     public final ByteBuffer buffer;
     public final ByteBuffer drained;
     public final List<Data> drainPoints = new ArrayList<>();
+    private long drainedByteCount;
 
     public static class Data {
         public final int position;
@@ -57,7 +58,13 @@ public class SpyByteBufferDestination implements ByteBufferDestination {
         buf.flip();
         drainPoints.add(new Data(buf.position(), buf.limit()));
         drained.put(buf);
+        drainedByteCount += buf.limit();
         buf.clear();
         return buf;
     }
+
+    @Override
+    public long size() {
+        return drainedByteCount + buffer.position();
+    }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/EncodingListAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/EncodingListAppender.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/EncodingListAppender.java
index 2e93a1c..48d9e4d 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/EncodingListAppender.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/EncodingListAppender.java
@@ -36,6 +36,11 @@ public class EncodingListAppender extends ListAppender {
         public ByteBuffer drain(final ByteBuffer buf) {
             throw new IllegalStateException("Unexpected message larger than 4096 bytes");
         }
+
+        @Override
+        public long size() {
+            return byteBuffer.position();
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java
index bab01ff..a233f93 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TextEncoderHelperBenchmark.java
@@ -80,6 +80,11 @@ public class TextEncoderHelperBenchmark {
             buf.clear();
             return buf;
         }
+
+        @Override
+        public long size() {
+            return count + buffer.position();
+        }
     }
 
     private static LogEvent createLogEvent() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6aea61e0/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/DemoAppender.java
----------------------------------------------------------------------
diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/DemoAppender.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/DemoAppender.java
index 2f3851d..d8a6ab5 100644
--- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/DemoAppender.java
+++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/DemoAppender.java
@@ -30,6 +30,7 @@ public class DemoAppender extends AbstractAppender implements ByteBufferDestinat
     private final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[4096]);
 
     public long checksum;
+    private long drained;
 
     public DemoAppender(Layout<?> layout) {
         super("demo", null, layout);
@@ -65,7 +66,13 @@ public class DemoAppender extends AbstractAppender implements ByteBufferDestinat
     public ByteBuffer drain(ByteBuffer buf) {
         buf.flip();
         consume(buf.array(), buf.position(), buf.limit());
+        drained += buf.limit();
         buf.clear();
         return buf;
     }
+
+    @Override
+    public long size() {
+        return drained + byteBuffer.position();
+    }
 }