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/06 20:11:42 UTC

[06/18] logging-log4j2 git commit: LOG4J2-1343 FileManager should honor the user-specified buffer size, otherwise it re-uses the garbage-free Layout mechanism defined in the superclass

LOG4J2-1343 FileManager should honor the user-specified buffer size, otherwise it re-uses the garbage-free Layout mechanism defined in the superclass


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

Branch: refs/heads/master
Commit: 3264915b9099712b35963648823778c5dd616d4a
Parents: dc78ca0
Author: rpopma <rp...@apache.org>
Authored: Thu Apr 7 02:59:04 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Thu Apr 7 02:59:04 2016 +0900

----------------------------------------------------------------------
 .../log4j/core/appender/FileManager.java        | 33 ++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3264915b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
index 4de34ad..d147127 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
@@ -23,12 +23,15 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Serializable;
+import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.layout.ByteBufferDestination;
+import org.apache.logging.log4j.core.util.Constants;
 
 
 /**
@@ -126,7 +129,7 @@ public class FileManager extends OutputStreamManager {
     public boolean isLocking() {
         return isLocking;
     }
-    
+
     /**
      * Returns the buffer size to use if the appender was configured with BufferedIO=true, otherwise returns a negative
      * number.
@@ -137,6 +140,29 @@ public class FileManager extends OutputStreamManager {
     }
 
     /**
+     * Returns whether the user requested IO to be buffered.
+     * @return whether the buffer size is larger than zero.
+     */
+    @Override
+    protected boolean isBufferedIO() {
+        return bufferSize > 0;
+    }
+
+    /**
+     * Returns a OutputStreamManagerDestination with the user-requested buffer size.
+     * @param immediateFlush the value to pass to the {@link #write(byte[], int, int, boolean)} method when the
+     *          ByteBufferDestination is {@link ByteBufferDestination#drain(ByteBuffer) drained}
+     * @return a OutputStreamManagerDestination with the user-requested buffer size
+     */
+    @Override
+    protected ByteBufferDestination createByteBufferDestination(final boolean immediateFlush) {
+        if (isBufferedIO()) {
+            return new OutputStreamManagerDestination(bufferSize, immediateFlush, this);
+        }
+        return new OutputStreamManagerDestination(immediateFlush, this);
+    }
+
+    /**
      * FileManager's content format is specified by: <code>Key: "fileURI" Value: provided "advertiseURI" param</code>.
      *
      * @return Map of content format keys supporting FileManager
@@ -202,7 +228,10 @@ public class FileManager extends OutputStreamManager {
             try {
                 os = new FileOutputStream(name, data.append);
                 int bufferSize = data.bufferSize;
-                if (data.bufferedIO) {
+
+                // 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; // signals to RollingFileManager not to use BufferedOutputStream