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/12/25 14:04:39 UTC

logging-log4j2 git commit: LOG4J2-1628 Fixed file locking regression in FileAppender introduced in 2.6.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 317f95084 -> 556cea011


LOG4J2-1628 Fixed file locking regression in FileAppender introduced in 2.6.


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

Branch: refs/heads/master
Commit: 556cea011dce61f3e9bc3f3518320080602b853c
Parents: 317f950
Author: rpopma <rp...@apache.org>
Authored: Sun Dec 25 23:04:30 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sun Dec 25 23:04:30 2016 +0900

----------------------------------------------------------------------
 .../log4j/core/appender/FileManager.java        | 40 ++++++++++++++++++--
 src/changes/changes.xml                         |  5 ++-
 2 files changed, 40 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/556cea01/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 6c0427a..17b4e13 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
@@ -59,7 +59,7 @@ public class FileManager extends OutputStreamManager {
 
     /**
      * @deprecated
-     * @since 2.6 
+     * @since 2.6
      */
     @Deprecated
     protected FileManager(final String fileName, final OutputStream os, final boolean append, final boolean locking,
@@ -73,8 +73,8 @@ public class FileManager extends OutputStreamManager {
         this.bufferSize = buffer.capacity();
     }
 
-    /** 
-     * @since 2.7 
+    /**
+     * @since 2.7
      */
     protected FileManager(final LoggerContext loggerContext, final String fileName, final OutputStream os, final boolean append, final boolean locking,
             final boolean createOnDemand, final String advertiseURI, final Layout<? extends Serializable> layout,
@@ -115,7 +115,7 @@ public class FileManager extends OutputStreamManager {
     protected OutputStream createOutputStream() throws FileNotFoundException {
         return new FileOutputStream(getFileName(), isAppend);
     }
-    
+
     @Override
     protected synchronized void write(final byte[] bytes, final int offset, final int length,
             final boolean immediateFlush) {
@@ -143,6 +143,38 @@ public class FileManager extends OutputStreamManager {
     }
 
     /**
+     * Overrides {@link OutputStreamManager#writeToDestination(byte[], int, int)} to add support for file locking.
+     *
+     * @param bytes the array containing data
+     * @param offset from where to write
+     * @param length how many bytes to write
+     * @since 2.6
+     */
+    protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
+        if (isLocking) {
+            try {
+                @SuppressWarnings("resource")
+                final FileChannel channel = ((FileOutputStream) getOutputStream()).getChannel();
+                /*
+                 * Lock the whole file. This could be optimized to only lock from the current file position. Note that
+                 * locking may be advisory on some systems and mandatory on others, so locking just from the current
+                 * position would allow reading on systems where locking is mandatory. Also, Java 6 will throw an
+                 * exception if the region of the file is already locked by another FileChannel in the same JVM.
+                 * Hopefully, that will be avoided since every file should have a single file manager - unless two
+                 * different files strings are configured that somehow map to the same file.
+                 */
+                try (final FileLock lock = channel.lock(0, Long.MAX_VALUE, false)) {
+                    super.writeToDestination(bytes, offset, length);
+                }
+            } catch (final IOException ex) {
+                throw new AppenderLoggingException("Unable to obtain lock on " + getName(), ex);
+            }
+        } else {
+            super.writeToDestination(bytes, offset, length);
+        }
+    }
+
+    /**
      * Returns the name of the File being managed.
      * @return The name of the File being managed.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/556cea01/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b3233c5..00f9d11 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.8" date="2016-MM-DD" description="GA Release 2.8">
+      <action issue="LOG4J2-1628" dev="rpopma" type="fix">
+        Fixed file locking regression in FileAppender introduced in 2.6.
+      </action>
       <action issue="LOG4J2-1744" dev="rpopma" type="fix">
         The custom logger Generate tool no longer requires the log4j-api module on the classpath.
       </action>
@@ -203,7 +206,7 @@
       </action>
       <action issue="LOG4J2-1751" dev="ggregory" type="update">
         Update liquibase-core from 3.5.1 to 3.5.3.
-      </action>      
+      </action>
       <action issue="LOG4J2-1302" dev="rpopma" type="update">
         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
       </action>