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 2020/02/22 21:06:37 UTC

[logging-log4j2] branch master updated: LOG4J2-2039 - RolloverFails when file matches pattern but index is too large.

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 7dd1e3c  LOG4J2-2039 - RolloverFails when file matches pattern but index is too large.
7dd1e3c is described below

commit 7dd1e3c741bd902769789f1183dfd7c4f6d44ec7
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Feb 22 14:06:20 2020 -0700

    LOG4J2-2039 - RolloverFails when file matches pattern but index is too large.
---
 .../core/appender/rolling/AbstractRolloverStrategy.java   |  9 +++++++--
 .../core/appender/rolling/RollingAppenderCountTest.java   | 15 ++++++++++++---
 src/changes/changes.xml                                   |  3 +++
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/AbstractRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/AbstractRolloverStrategy.java
index fb12cc2..375dc09 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/AbstractRolloverStrategy.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/AbstractRolloverStrategy.java
@@ -127,8 +127,13 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy {
             for (final Path entry: stream) {
                 final Matcher matcher = pattern.matcher(entry.toFile().getName());
                 if (matcher.matches() && !entry.equals(current)) {
-                    final Integer index = Integer.parseInt(matcher.group(1));
-                    eligibleFiles.put(index, entry);
+                    try {
+                        final Integer index = Integer.parseInt(matcher.group(1));
+                        eligibleFiles.put(index, entry);
+                    } catch (NumberFormatException ex) {
+                        LOGGER.debug("Ignoring file {} which matches pattern but the index is invalid.",
+                                entry.toFile().getName());
+                    }
                 }
             }
         } catch (final IOException ioe) {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCountTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCountTest.java
index 4852078..2ce95c9 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCountTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderCountTest.java
@@ -30,6 +30,7 @@ import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.Objects;
 
 import static org.junit.Assert.assertEquals;
@@ -39,9 +40,11 @@ import static org.junit.Assert.assertEquals;
  */
 public class RollingAppenderCountTest {
 
+    private static final String SOURCE = "src/test/resources/__files";
     private static final String DIR = "target/rolling_count";
     private static final String CONFIG = "log4j-rolling-count.xml";
-    private static final String FILENAME = "rolling_test.log";
+    private static final String FILENAME = "onStartup.log";
+    private static final String TARGET = "rolling_test.log.";
 
     private Logger logger;
 
@@ -59,7 +62,7 @@ public class RollingAppenderCountTest {
 
     @BeforeClass
     public static void beforeClass() throws Exception {
-        if (Files.exists(Paths.get("target/onStartup"))) {
+        if (Files.exists(Paths.get(DIR))) {
             try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
                 for (final Path path : directoryStream) {
                     Files.delete(path);
@@ -67,12 +70,18 @@ public class RollingAppenderCountTest {
                 Files.delete(Paths.get(DIR));
             }
         }
+        File dir = new File(DIR);
+        if (!dir.exists()) {
+            Files.createDirectory(new File(DIR).toPath());
+        }
+        Path target = Paths.get(DIR, TARGET + System.currentTimeMillis());
+        Files.copy(Paths.get(SOURCE, FILENAME), target, StandardCopyOption.COPY_ATTRIBUTES);
     }
 
     @AfterClass
     public static void afterClass() throws Exception {
         int count = Objects.requireNonNull(new File(DIR).listFiles()).length;
-        assertEquals("Expected 16 files, got " + count, 16, count);
+        assertEquals("Expected 16 files, got " + count, 17, count);
         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
             for (final Path path : directoryStream) {
                 Files.delete(path);
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4ff934e..727560e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -166,6 +166,9 @@
       </action>
     </release>
     <release version="2.13.1" date="2019-MM-DD" description="GA Release 2.13.1">
+      <action issue="LOG4J2-2039" dev="rgoers" type="fix">
+        RolloverFails then file matches pattern but index is too large.
+      </action>
       <action issue="LOG4J2-2784" dev="rgoers" type="fix">
         Counter stuck at 10 and overwriting files when leading zeros used in the file pattern count.
       </action>