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 2021/11/21 00:10:25 UTC

[logging-log4j2] branch master updated: LOG4J2-3168 - Fix bug when file names contain regex characters.

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 9df31f7  LOG4J2-3168 - Fix bug when file names contain regex characters.
9df31f7 is described below

commit 9df31f73b62ba23e335d548843547f72b3684a66
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Nov 20 17:10:15 2021 -0700

    LOG4J2-3168 - Fix bug when file names contain regex characters.
---
 .../core/appender/rolling/AbstractRolloverStrategy.java   |  10 ++++++++--
 .../log4j/core/appender/rolling/EligibleFilesTest.java    |  14 ++++++++++++++
 .../rolloverPath/log4j.20211028T194500+0200.1.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.10.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.11.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.12.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.13.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.14.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.15.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.16.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.17.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.18.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.19.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.2.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.20.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.21.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.22.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.23.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.24.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.25.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.26.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.27.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.28.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.29.log.gz     | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.3.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.30.log        |   1 +
 .../rolloverPath/log4j.20211028T194500+0200.4.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.5.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.6.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.7.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.8.log.gz      | Bin 0 -> 47 bytes
 .../rolloverPath/log4j.20211028T194500+0200.9.log.gz      | Bin 0 -> 47 bytes
 src/changes/changes.xml                                   |   3 +++
 33 files changed, 26 insertions(+), 2 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 da3ad10..31c0294 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
@@ -115,10 +115,16 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy {
         final Path dir = parent.toPath();
         String fileName = file.getName();
         final int suffixLength = suffixLength(fileName);
+        // use Pattern.quote to treat all initial parts of the fileName as literal
+        // this fixes issues with filenames containing 'magic' regex characters
         if (suffixLength > 0) {
-            fileName = fileName.substring(0, fileName.length() - suffixLength) + ".*";
+            fileName = Pattern.quote(fileName.substring(0, fileName.length() - suffixLength)) + ".*";
+        } else {
+            fileName = Pattern.quote(fileName);
         }
-        final String filePattern = fileName.replaceFirst("0?\\u0000", "(0?\\\\d+)");
+        // since we insert a pattern inside a regex escaped string,
+        // surround it with quote characters so that (\d) is treated as a pattern and not a literal
+        final String filePattern = fileName.replaceFirst("0?\\u0000", "\\\\E(0?\\\\d+)\\\\Q");
         final Pattern pattern = Pattern.compile(filePattern);
         final Path current = currentFile.length() > 0 ? new File(currentFile).toPath() : null;
         LOGGER.debug("Current file: {}", currentFile);
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
index 4b99248..537c274 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/EligibleFilesTest.java
@@ -38,6 +38,15 @@ public class EligibleFilesTest {
         assertEquals(30, files.size(), "Incorrect number of files found. Should be 30, was " + files.size());
     }
 
+    @Test
+    public void runTestWithPlusCharacter() throws Exception {
+        final String path = "target/test-classes/rolloverPath/log4j.20211028T194500+0200." + NotANumber.VALUE + ".log.gz";
+        final TestRolloverStrategy strategy = new TestRolloverStrategy();
+        final Map<Integer, Path> files = strategy.findFilesWithPlusInPath(path);
+        assertTrue(files.size() > 0, "No files found");
+        assertEquals(30, files.size(), "Incorrect number of files found. Should be 30, was " + files.size());
+    }
+
     private static class TestRolloverStrategy extends AbstractRolloverStrategy {
 
         public TestRolloverStrategy() {
@@ -52,5 +61,10 @@ public class EligibleFilesTest {
         public Map<Integer, Path> findFilesInPath(final String path) {
             return getEligibleFiles(path, "log4j.txt.%d{yyyyMMdd}-%i.gz");
         }
+
+        public Map<Integer, Path> findFilesWithPlusInPath(final String path) {
+            // timezone might expand to "+0200", because of '+' we have to be careful when working with regex
+            return getEligibleFiles(path, "log4j.txt.%d{yyyyMMdd'T'HHmmssZ}.%i.log.gz");
+        }
     }
 }
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.1.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.1.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.1.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.10.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.10.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.10.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.11.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.11.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.11.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.12.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.12.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.12.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.13.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.13.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.13.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.14.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.14.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.14.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.15.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.15.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.15.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.16.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.16.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.16.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.17.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.17.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.17.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.18.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.18.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.18.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.19.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.19.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.19.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.2.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.2.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.2.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.20.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.20.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.20.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.21.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.21.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.21.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.22.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.22.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.22.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.23.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.23.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.23.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.24.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.24.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.24.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.25.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.25.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.25.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.26.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.26.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.26.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.27.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.27.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.27.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.28.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.28.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.28.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.29.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.29.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.29.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.3.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.3.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.3.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.30.log b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.30.log
new file mode 100644
index 0000000..8d1c8b6
--- /dev/null
+++ b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.30.log
@@ -0,0 +1 @@
+ 
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.4.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.4.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.4.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.5.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.5.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.5.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.6.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.6.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.6.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.7.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.7.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.7.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.8.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.8.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.8.log.gz differ
diff --git a/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.9.log.gz b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.9.log.gz
new file mode 100644
index 0000000..8fa0ab8
Binary files /dev/null and b/log4j-core/src/test/resources/rolloverPath/log4j.20211028T194500+0200.9.log.gz differ
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ec46c70..efb48d5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -2969,6 +2969,9 @@
       <action issue="LOG4J2-1384" dev="ggregory" type="update">
         Update Apache Commons CSV from 1.2 to 1.3.
       </action>
+      <action issue="LOG4J2-3168" dev="rgoers" type="fix" due-to="Benjamin Wöster">
+        Fix bug when file names contain regex characters.
+      </action>
       <action issue="LOG4J2-3110" dev="rgoers" type="fix" due-to="Arturo Bernal">
         Fix the number of {}-placeholders in the string literal argument does not match the number of other arguments to the logging call.
       </action>