You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2012/08/30 08:34:17 UTC

svn commit: r1378813 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/ core/src/test/java/org/apache/logging/log4j/core/appender/rolling/ core/src/test/resources/ src/changes/

Author: rgoers
Date: Thu Aug 30 06:34:16 2012
New Revision: 1378813

URL: http://svn.apache.org/viewvc?rev=1378813&view=rev
Log:
Fix for LOG4J2-71

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
    logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling3.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java?rev=1378813&r1=1378812&r2=1378813&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java Thu Aug 30 06:34:16 2012
@@ -16,6 +16,9 @@
  */
 package org.apache.logging.log4j.core.appender.rolling.helper;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -27,6 +30,9 @@ import java.nio.channels.FileChannel;
  * File rename action.
  */
 public final class FileRenameAction extends ActionBase {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
     /**
      * Source.
      */
@@ -74,17 +80,31 @@ public final class FileRenameAction exte
      */
     public static boolean execute(final File source, final File destination, boolean renameEmptyFiles) {
         if (renameEmptyFiles || (source.length() > 0)) {
+            File parent = destination.getParentFile();
+            if (!parent.exists()) {
+                if (!parent.mkdirs()) {
+                    LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
+                    return false;
+                }
+            }
             try {
-
-                boolean result = source.renameTo(destination);
-                //System.out.println("Rename of " + source.getName() + " to " + destination.getName() + ": " + result);
-                return result;
+                if (!source.renameTo(destination)) {
+                    try {
+                        copyFile(source, destination);
+                        return source.delete();
+                    } catch (IOException iex) {
+                        LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
+                            destination.getAbsolutePath(), iex.getMessage());
+                    }
+                }
+                return true;
             } catch (Exception ex) {
                 try {
                     copyFile(source, destination);
                     return source.delete();
                 } catch (IOException iex) {
-                    iex.printStackTrace();
+                    LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
+                        destination.getAbsolutePath(), iex.getMessage());
                 }
             }
         }

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java?rev=1378813&r1=1378812&r2=1378813&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java (original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderTimeAndSizeTest.java Thu Aug 30 06:34:16 2012
@@ -35,7 +35,7 @@ import static org.junit.Assert.assertTru
 public class RollingAppenderTimeAndSizeTest {
 
     private static final String CONFIG = "log4j-rolling3.xml";
-    private static final String DIR = "target/rolling3";
+    private static final String DIR = "target/rolling3/test";
 
     org.apache.logging.log4j.Logger logger = LogManager.getLogger(RollingAppenderTimeAndSizeTest.class.getName());
 
@@ -49,7 +49,7 @@ public class RollingAppenderTimeAndSizeT
 
     @AfterClass
     public static void cleanupClass() {
-        deleteDir();
+        //deleteDir();
         System.clearProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
         LoggerContext ctx = (LoggerContext) LogManager.getContext();
         ctx.reconfigure();

Modified: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling3.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling3.xml?rev=1378813&r1=1378812&r2=1378813&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling3.xml (original)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-rolling3.xml Thu Aug 30 06:34:16 2012
@@ -26,7 +26,7 @@
     <Console name="STDOUT">
       <PatternLayout pattern="%m%n"/>
     </Console>
-    <RollingFile name="RollingFile" fileName="${filename}" filePattern="target/rolling3/test1-%d{MM-dd-yy-HH-mm}-%i.log.gz">
+    <RollingFile name="RollingFile" fileName="${filename}" filePattern="target/rolling3/test/test1-%d{MM-dd-yy-HH-mm}-%i.log.gz">
       <PatternLayout>
         <pattern>%d %p %C{1.} [%t] %m%n</pattern>
       </PatternLayout>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1378813&r1=1378812&r2=1378813&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Thu Aug 30 06:34:16 2012
@@ -26,6 +26,9 @@
       <action dev="rgoers" type="update">
         Update the versions of SLF4J and Logback.
       </action>
+      <action issue="LOG4J2-71" dev="rgoers" type="fix">
+        FileRenameAction did not create the parent directories of the archive files causing the rollover to fail.
+      </action>
     </release>
     <release version="2.0-alpha2" date="2012-08-24" description="Bug fixes and minor enhancements">
       <action issue="LOG4J2-70" dev="rgoers" type="add">