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

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


The following commit(s) were added to refs/heads/release-2.x by this push:
     new c245c52  LOG4J2-2712 - The rolling file appenders would fail to 
compress the file after rollover if the file name matched the file pattern
c245c52 is described below

commit c245c5221c0517e7d5ee5656abacf01b1dc2af78
Author: Ralph Goers <[email protected]>
AuthorDate: Fri Nov 8 22:08:41 2019 -0700

    LOG4J2-2712 - The rolling file appenders would fail to compress the file 
after rollover if the file name matched the file pattern
---
 .../appender/rolling/AbstractRolloverStrategy.java | 18 +++++--
 .../appender/rolling/DefaultRolloverStrategy.java  |  2 +-
 .../core/appender/rolling/RollingFileManager.java  | 12 ++++-
 .../appender/rolling/RollingAppenderSizeTest.java  |  1 +
 .../test/resources/log4j-rolling-numbered-gz.xml   | 59 ++++++++++++++++++++++
 src/changes/changes.xml                            |  4 ++
 6 files changed, 89 insertions(+), 7 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 a69e220..b14780e 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
@@ -91,14 +91,22 @@ public abstract class AbstractRolloverStrategy implements 
RolloverStrategy {
         final StringBuilder buf = new StringBuilder();
         final String pattern = manager.getPatternProcessor().getPattern();
         manager.getPatternProcessor().formatFileName(strSubstitutor, buf, 
NotANumber.NAN);
-        return getEligibleFiles(buf.toString(), pattern, isAscending);
+        final String fileName = manager.isDirectWrite() ? "" : 
manager.getFileName();
+        return getEligibleFiles(fileName, buf.toString(), pattern, 
isAscending);
     }
 
     protected SortedMap<Integer, Path> getEligibleFiles(final String path, 
final String pattern) {
-        return getEligibleFiles(path, pattern, true);
+        return getEligibleFiles("", path, pattern, true);
     }
 
-    protected SortedMap<Integer, Path> getEligibleFiles(final String path, 
final String logfilePattern, final boolean isAscending) {
+    @Deprecated
+    protected SortedMap<Integer, Path> getEligibleFiles(final String path, 
final String logfilePattern,
+            final boolean isAscending) {
+        return getEligibleFiles("", path, logfilePattern, isAscending);
+    }
+
+    protected SortedMap<Integer, Path> getEligibleFiles(final String 
currentFile, final String path,
+            final String logfilePattern, final boolean isAscending) {
         final TreeMap<Integer, Path> eligibleFiles = new TreeMap<>();
         final File file = new File(path);
         File parent = file.getParentFile();
@@ -118,11 +126,13 @@ public abstract class AbstractRolloverStrategy implements 
RolloverStrategy {
         }
         final String filePattern = fileName.replace(NotANumber.VALUE, 
"(\\d+)");
         final Pattern pattern = Pattern.compile(filePattern);
+        final Path current = currentFile.length() > 0 ? new 
File(currentFile).toPath() : null;
+        LOGGER.debug("Current file: {}", currentFile);
 
         try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
             for (final Path entry: stream) {
                 final Matcher matcher = 
pattern.matcher(entry.toFile().getName());
-                if (matcher.matches()) {
+                if (matcher.matches() && !entry.equals(current)) {
                     final Integer index = Integer.parseInt(matcher.group(1));
                     eligibleFiles.put(index, entry);
                 }
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
index fab14f7..a5f3dbf 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java
@@ -470,7 +470,7 @@ public class DefaultRolloverStrategy extends 
AbstractRolloverStrategy {
         // Retrieve the files in descending order, so the highest key will be 
first.
         final SortedMap<Integer, Path> eligibleFiles = 
getEligibleFiles(manager, false);
         final int maxFiles = highIndex - lowIndex + 1;
-
+        LOGGER.debug("Eligible files: {}", eligibleFiles);
         while (eligibleFiles.size() >= maxFiles) {
             try {
                 final Integer key = eligibleFiles.firstKey();
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 6867855..89a4c4c 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
@@ -71,6 +71,7 @@ public class RollingFileManager extends FileManager {
     private volatile boolean initialized = false;
     private volatile String fileName;
     private final FileExtension fileExtension;
+    private final boolean directWrite;
 
     /* This executor pool will create a new Thread for every work async action 
to be performed. Using it allows
        us to make sure all the Threads are completed when the Manager is 
stopped. */
@@ -109,6 +110,7 @@ public class RollingFileManager extends FileManager {
         this.patternProcessor = new PatternProcessor(pattern);
         this.patternProcessor.setPrevFileTime(initialTime);
         this.fileName = fileName;
+        this.directWrite = rolloverStrategy instanceof 
DirectWriteRolloverStrategy;
         this.fileExtension = FileExtension.lookupForFile(pattern);
     }
 
@@ -126,6 +128,7 @@ public class RollingFileManager extends FileManager {
         this.patternProcessor = new PatternProcessor(pattern);
         this.patternProcessor.setPrevFileTime(initialTime);
         this.fileName = fileName;
+        this.directWrite = rolloverStrategy instanceof 
DirectWriteRolloverStrategy;
         this.fileExtension = FileExtension.lookupForFile(pattern);
     }
 
@@ -147,6 +150,7 @@ public class RollingFileManager extends FileManager {
         this.patternProcessor = new PatternProcessor(pattern);
         this.patternProcessor.setPrevFileTime(initialTime);
         this.fileName = fileName;
+        this.directWrite = rolloverStrategy instanceof 
DirectWriteRolloverStrategy;
         this.fileExtension = FileExtension.lookupForFile(pattern);
     }
 
@@ -159,7 +163,7 @@ public class RollingFileManager extends FileManager {
             if (triggeringPolicy instanceof LifeCycle) {
                 ((LifeCycle) triggeringPolicy).start();
             }
-            if (rolloverStrategy instanceof DirectFileRolloverStrategy) {
+            if (directWrite) {
                 // LOG4J2-2485: Initialize size from the most recently written 
file.
                 File file = new File(getFileName());
                 if (file.exists()) {
@@ -213,12 +217,16 @@ public class RollingFileManager extends FileManager {
      */
     @Override
     public String getFileName() {
-        if (rolloverStrategy instanceof DirectFileRolloverStrategy) {
+        if (directWrite) {
             fileName = ((DirectFileRolloverStrategy) 
rolloverStrategy).getCurrentFileName(this);
         }
         return fileName;
     }
 
+    public boolean isDirectWrite() {
+        return directWrite;
+    }
+
     public FileExtension getFileExtension() {
         return fileExtension;
     }
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
index 8e32377..625c2eb 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
@@ -75,6 +75,7 @@ public class RollingAppenderSizeTest {
                 // @formatter:off
                {"log4j-rolling-gz-lazy.xml", ".gz", true},
                {"log4j-rolling-gz.xml", ".gz", false},
+               {"log4j-rolling-numbered-gz.xml", ".gz", false},
                {"log4j-rolling-zip-lazy.xml", ".zip", true},
                {"log4j-rolling-zip.xml", ".zip", false},
                 // Apache Commons Compress
diff --git a/log4j-core/src/test/resources/log4j-rolling-numbered-gz.xml 
b/log4j-core/src/test/resources/log4j-rolling-numbered-gz.xml
new file mode 100644
index 0000000..f7aeb13
--- /dev/null
+++ b/log4j-core/src/test/resources/log4j-rolling-numbered-gz.xml
@@ -0,0 +1,59 @@
+<?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="WARN" name="XMLConfigTest">
+  <Properties>
+    <Property name="filename">target/rolling1/rolling0-0.log</Property>
+  </Properties>
+  <ThresholdFilter level="debug"/>
+
+  <Appenders>
+    <Console name="STDOUT">
+      <PatternLayout pattern="%m%n"/>
+    </Console>
+    <RollingFile name="RollingFile" fileName="${filename}"
+                 filePattern="target/rolling1/rolling0-%i.log.gz">
+      <PatternLayout>
+        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
+      </PatternLayout>
+      <SizeBasedTriggeringPolicy size="500" />
+      <DefaultRolloverStrategy max="7" min="1" fileIndex="min"/>
+    </RollingFile>
+    <List name="List">
+      <ThresholdFilter level="error"/>
+    </List>
+  </Appenders>
+
+  <Loggers>
+    <Logger name="org.apache.logging.log4j.test1" level="debug" 
additivity="false">
+      <ThreadContextMapFilter>
+        <KeyValuePair key="test" value="123"/>
+      </ThreadContextMapFilter>
+      <AppenderRef ref="STDOUT"/>
+    </Logger>>
+
+    <Logger name="org.apache.logging.log4j.core.appender.rolling" 
level="debug" additivity="false">
+      <AppenderRef ref="RollingFile"/>
+    </Logger>>
+
+    <Root level="error">
+      <AppenderRef ref="STDOUT"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9e79004..57bede2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,10 @@
          - "remove" - Removed
     -->
     <release version="2.13.0" date="2019-MM-DD" description="GA Release 
2.13.0">
+      <action issue="LOG4J2-2712" dev="rgoers" type="fix">
+        The rolling file appenders would fail to compress the file after 
rollover if the file name matched the
+        file pattern.
+      </action>
       <action issue="LOG4J2-2716" dev="rgoers" type="add">
         Add the ability to lookup Kubernetes attributes in the Log4j 
configuration. Allow Log4j properties to
         be retrieved from the Spring environment if it is available.

Reply via email to