Repository: logging-log4j2
Updated Branches:
  refs/heads/master fa9096070 -> 0c556aecb


LOG4J-1420 - RollingRandomAccessFileManager was not properly rolling over on 
startup and was getting a NullPointerException


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

Branch: refs/heads/master
Commit: 0c556aecb20f807d2838073add3b29be61a5cd3b
Parents: fa90960
Author: Ralph Goers <[email protected]>
Authored: Sat Jun 11 18:48:04 2016 -0700
Committer: Ralph Goers <[email protected]>
Committed: Sat Jun 11 18:48:04 2016 -0700

----------------------------------------------------------------------
 .../core/appender/RollingFileAppender.java      |   2 +
 .../RollingRandomAccessFileAppender.java        |   2 +
 .../appender/rolling/RollingFileManager.java    |   3 +
 .../rolling/RollingRandomAccessFileManager.java |   6 +-
 .../rolling/OnStartupTriggeringPolicyTest.java  |   1 +
 .../RandomRollingAppenderOnStartupTest.java     | 109 +++++++++++++++++++
 log4j-core/src/test/resources/log4j-test5.xml   |  36 ++++++
 src/changes/changes.xml                         |   3 +
 8 files changed, 160 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
index 21d0b91..88bbb9a 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingFileAppender.java
@@ -194,6 +194,8 @@ public final class RollingFileAppender extends 
AbstractOutputStreamAppender<Roll
             return null;
         }
 
+        manager.initialize();
+
         return new RollingFileAppender(name, layout, filter, manager, 
fileName, filePattern,
                 ignoreExceptions, isFlush, isAdvertise ? 
config.getAdvertiser() : null);
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingRandomAccessFileAppender.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingRandomAccessFileAppender.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingRandomAccessFileAppender.java
index dc92d7e..67777e6 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingRandomAccessFileAppender.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RollingRandomAccessFileAppender.java
@@ -210,6 +210,8 @@ public final class RollingRandomAccessFileAppender extends 
AbstractOutputStreamA
             return null;
         }
 
+        manager.initialize();
+
         return new RollingRandomAccessFileAppender(name, layout, filter, 
manager,
                 fileName, filePattern, ignoreExceptions, isFlush, bufferSize,
                 isAdvertise ? config.getAdvertiser() : null);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/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 ade19b3..44fbbd6 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
@@ -74,6 +74,9 @@ public class RollingFileManager extends FileManager {
         this.triggeringPolicy = triggeringPolicy;
         this.rolloverStrategy = rolloverStrategy;
         this.patternProcessor = new PatternProcessor(pattern);
+    }
+
+    public void initialize() {
         triggeringPolicy.initialize(this);
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
index 7a3e43d..d51b65c 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
@@ -66,8 +66,10 @@ public class RollingRandomAccessFileManager extends 
RollingFileManager {
             return;
         }
         try {
-            // write to the file, not to the buffer: the buffer may not be 
empty
-            randomAccessFile.write(header, 0, header.length);
+            if (randomAccessFile.length() == 0) {
+                // write to the file, not to the buffer: the buffer may not be 
empty
+                randomAccessFile.write(header, 0, header.length);
+            }
         } catch (final IOException e) {
             logError("unable to write header", e);
         }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
index 7fd2428..bfec1c8 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/OnStartupTriggeringPolicyTest.java
@@ -58,6 +58,7 @@ public class OnStartupTriggeringPolicyTest {
         OnStartupTriggeringPolicy policy = 
OnStartupTriggeringPolicy.createPolicy();
         RollingFileManager manager = 
RollingFileManager.getFileManager(TARGET_FILE, TARGET_PATTERN, true, false,
                 policy, strategy, null, layout, 8192, true);
+        manager.initialize();
         assertTrue(Files.exists(target));
         assertTrue(Files.size(target) == 0);
         assertTrue(Files.exists(rolled));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
new file mode 100644
index 0000000..013e4c6
--- /dev/null
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RandomRollingAppenderOnStartupTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.appender.rolling;
+
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collection;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+@RunWith(Parameterized.class)
+public class RandomRollingAppenderOnStartupTest {
+
+    private static final String DIR = "target/rolling1";
+
+    private final String fileExtension;
+
+    private Logger logger;
+
+    private static volatile int counter = 0;
+
+    @Parameterized.Parameters(name = "{0} \u2192 {1}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] { //
+                // @formatter:off
+                {"log4j-test5.xml", ".gz"},
+                {"log4j-test5.xml", ".gz"},});
+                // @formatter:on
+    }
+
+    @Rule
+    public LoggerContextRule init;
+
+    public RandomRollingAppenderOnStartupTest(final String configFile, final 
String fileExtension) {
+        this.fileExtension = fileExtension;
+        this.init = new LoggerContextRule(configFile);
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        this.logger = 
this.init.getLogger(RandomRollingAppenderOnStartupTest.class.getName());
+    }
+
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        if (Files.exists(Paths.get("target/onStartup"))) {
+            try (DirectoryStream<Path> directoryStream = 
Files.newDirectoryStream(Paths.get("target/onStartup"))) {
+                for (Path path : directoryStream) {
+                    Files.delete(path);
+                }
+                Files.delete(Paths.get("target/onStartup"));
+            }
+        }
+    }
+
+    @AfterClass
+    public static void afterClass() throws Exception {
+        long size = 0;
+        try (DirectoryStream<Path> directoryStream = 
Files.newDirectoryStream(Paths.get("target/onStartup"))) {
+            for (Path path : directoryStream) {
+                if (size == 0) {
+                    size = Files.size(path);
+                } else {
+                    assertTrue(size == Files.size(path));
+                }
+                Files.delete(path);
+            }
+            Files.delete(Paths.get("target/onStartup"));
+        }
+    }
+
+    @Test
+    public void testAppender() throws Exception {
+        System.out.println("Pass " + counter++);
+        for (int i = 0; i < 100; ++i) {
+            logger.debug("This is test message number " + i);
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/log4j-core/src/test/resources/log4j-test5.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/log4j-test5.xml 
b/log4j-core/src/test/resources/log4j-test5.xml
new file mode 100644
index 0000000..9d11454
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-test5.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<Configuration status="ERROR" name="XMLConfigTest" monitorInterval="1">
+  <Appenders>
+    <RollingRandomAccessFile name="RollingFile" 
fileName="target/onStartup/onStartup.log"
+                 
filePattern="target/onStartup/onStartup-%d{MM-dd-yyyy}-%i.log">
+      <HTMLLayout title="test"/>
+      <Policies>
+        <OnStartupTriggeringPolicy />
+      </Policies>
+    </RollingRandomAccessFile>
+  </Appenders>
+
+  <Loggers>
+    <Root level="trace">
+      <AppenderRef ref="RollingFile"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0c556aec/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c5ea71e..8e41680 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.6.2" date="2016-MM-DD" description="GA Release 2.6.2">
+      <action issue="LOG4J2-1420" dev="rgoers" type="fix">
+        RollingRandomAccessFileManager was not properly rolling over on 
startup and was getting a NullPointerException.
+      </action>
       <action issue="LOG4J2-1417" dev="rpopma" type="fix">
         Fixed issue where Unbox utility ignored the value 
Constants.ENABLE_THREADLOCALS and always stored non-JDK classes in ThreadLocals.
       </action>

Reply via email to