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/12 18:33:46 UTC

[06/17] logging-log4j2 git commit: LOG4J2-1343 RollingFileManager: removed BufferedOutputStream, use ByteBuffer instead

LOG4J2-1343 RollingFileManager: removed BufferedOutputStream, use ByteBuffer instead


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

Branch: refs/heads/master
Commit: 5c5209beb1bb62d87b952f4d599d8b36b7bf212b
Parents: 4855eb8
Author: rpopma <rp...@apache.org>
Authored: Tue Apr 12 02:37:57 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Apr 12 02:37:57 2016 +0900

----------------------------------------------------------------------
 .../appender/rolling/RollingFileManager.java    | 68 ++++++++++----------
 1 file changed, 35 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5c5209be/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
index 560ec6c..d0b0735 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
@@ -16,13 +16,13 @@
  */
 package org.apache.logging.log4j.core.appender.rolling;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Serializable;
+import java.nio.ByteBuffer;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 
@@ -55,11 +55,20 @@ public class RollingFileManager extends FileManager {
     private static final AtomicReferenceFieldUpdater<RollingFileManager, RolloverStrategy> rolloverStrategyUpdater =
             AtomicReferenceFieldUpdater.newUpdater(RollingFileManager.class, RolloverStrategy.class, "rolloverStrategy");
 
+    @Deprecated
     protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
             final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
             final RolloverStrategy rolloverStrategy, final String advertiseURI,
             final Layout<? extends Serializable> layout, final int bufferSize, final boolean writeHeader) {
-        super(fileName, os, append, false, advertiseURI, layout, bufferSize, writeHeader);
+        this(fileName, pattern, os, append, size, time, triggeringPolicy, rolloverStrategy, advertiseURI, layout,
+                writeHeader, ByteBuffer.wrap(new byte[DEFAULT_BUFFER_SIZE]));
+    }
+
+    protected RollingFileManager(final String fileName, final String pattern, final OutputStream os,
+            final boolean append, final long size, final long time, final TriggeringPolicy triggeringPolicy,
+            final RolloverStrategy rolloverStrategy, final String advertiseURI,
+            final Layout<? extends Serializable> layout, final boolean writeHeader, final ByteBuffer buffer) {
+        super(fileName, os, append, false, advertiseURI, layout, writeHeader, buffer);
         this.size = size;
         this.initialTime = time;
         this.triggeringPolicy = triggeringPolicy;
@@ -83,27 +92,32 @@ public class RollingFileManager extends FileManager {
      */
     public static RollingFileManager getFileManager(final String fileName, final String pattern, final boolean append,
             final boolean bufferedIO, final TriggeringPolicy policy, final RolloverStrategy strategy,
-            final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize) {
+            final String advertiseURI, final Layout<? extends Serializable> layout, final int bufferSize,
+            final boolean immediateFlush) {
 
         return (RollingFileManager) getManager(fileName, new FactoryData(pattern, append,
-            bufferedIO, policy, strategy, advertiseURI, layout, bufferSize), factory);
+            bufferedIO, policy, strategy, advertiseURI, layout, bufferSize, immediateFlush), factory);
     }
 
+    // override to make visible for unit tests
     @Override
-    protected synchronized void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush) {
-        size += length;
+    protected synchronized void write(final byte[] bytes, final int offset, final int length,
+            final boolean immediateFlush) {
         super.write(bytes, offset, length, immediateFlush);
     }
 
+    @Override
+    protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
+        size += length;
+        super.writeToDestination(bytes, offset, length);
+    }
+
     /**
      * Returns the current size of the file.
      * @return The size of the file in bytes.
      */
     public long getFileSize() {
-        if (Constants.ENABLE_DIRECT_ENCODERS) {
-            return size + getByteBufferDestination().getByteBuffer().position();
-        }
-        return size;
+        return size + byteBuffer.position();
     }
 
     /**
@@ -138,14 +152,7 @@ public class RollingFileManager extends FileManager {
 
     protected void createFileAfterRollover() throws IOException  {
         final OutputStream os = new FileOutputStream(getFileName(), isAppend());
-
-        // when the garbage-free Layout encode mechanism is used
-        // we use a ByteBuffer instead of BufferedOutputStream
-        if (!Constants.ENABLE_DIRECT_ENCODERS && isBufferedIO()) {
-            setOutputStream(new BufferedOutputStream(os, getBufferSize()));
-        } else {
-            setOutputStream(os);
-        }
+        setOutputStream(os);
     }
 
     /**
@@ -156,14 +163,12 @@ public class RollingFileManager extends FileManager {
         return patternProcessor;
     }
 
-    public void setTriggeringPolicy(final TriggeringPolicy triggeringPolicy)
-    {
+    public void setTriggeringPolicy(final TriggeringPolicy triggeringPolicy) {
         triggeringPolicy.initialize(this);
         triggeringPolicyUpdater.compareAndSet(this, this.triggeringPolicy, triggeringPolicy);
     }
 
-    public void setRolloverStrategy(final RolloverStrategy rolloverStrategy)
-    {
+    public void setRolloverStrategy(final RolloverStrategy rolloverStrategy) {
         rolloverStrategyUpdater.compareAndSet(this, this.rolloverStrategy, rolloverStrategy);
     }
 
@@ -307,6 +312,7 @@ public class RollingFileManager extends FileManager {
         private final boolean append;
         private final boolean bufferedIO;
         private final int bufferSize;
+        private final boolean immediateFlush;
         private final TriggeringPolicy policy;
         private final RolloverStrategy strategy;
         private final String advertiseURI;
@@ -320,10 +326,11 @@ public class RollingFileManager extends FileManager {
          * @param advertiseURI
          * @param layout The Layout.
          * @param bufferSize the buffer size
+         * @param immediateFlush flush on every write or not
          */
         public FactoryData(final String pattern, final boolean append, final boolean bufferedIO,
                 final TriggeringPolicy policy, final RolloverStrategy strategy, final String advertiseURI,
-                final Layout<? extends Serializable> layout, final int bufferSize) {
+                final Layout<? extends Serializable> layout, final int bufferSize, final boolean immediateFlush) {
             this.pattern = pattern;
             this.append = append;
             this.bufferedIO = bufferedIO;
@@ -332,6 +339,7 @@ public class RollingFileManager extends FileManager {
             this.strategy = strategy;
             this.advertiseURI = advertiseURI;
             this.layout = layout;
+            this.immediateFlush = immediateFlush;
         }
 
         public TriggeringPolicy getTriggeringPolicy()
@@ -408,18 +416,12 @@ public class RollingFileManager extends FileManager {
             OutputStream os;
             try {
                 os = new FileOutputStream(name, data.append);
-                int bufferSize = data.bufferSize;
-
-                // when the garbage-free Layout encode mechanism is used
-                // we use a ByteBuffer instead of BufferedOutputStream
-                if (!Constants.ENABLE_DIRECT_ENCODERS && data.bufferedIO) {
-                    os = new BufferedOutputStream(os, bufferSize);
-                } else {
-                    bufferSize = -1; // negative buffer size signals bufferedIO was configured false
-                }
+                final int actualSize = data.bufferedIO ? data.bufferSize : DEFAULT_BUFFER_SIZE;
+                final ByteBuffer buffer = ByteBuffer.wrap(new byte[actualSize]);
+
                 final long time = file.lastModified(); // LOG4J2-531 create file first so time has valid value
                 return new RollingFileManager(name, data.pattern, os, data.append, size, time, data.policy,
-                    data.strategy, data.advertiseURI, data.layout, bufferSize, writeHeader);
+                    data.strategy, data.advertiseURI, data.layout, writeHeader, buffer);
             } catch (final FileNotFoundException ex) {
                 LOGGER.error("FileManager (" + name + ") " + ex, ex);
             }