Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1821

(not only) default rollover strategy is *broken*

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.8
    • Fix Version/s: 2.8.1
    • Component/s: Core
    • Labels:
    • Environment:

      Java 1.8

    • Flags:
      Important

      Description

      A previously working <DefaultRolloverStrategy max="10" /> is broken on v2.8, while working against 2.7, as only one rollover file is available. here is a MCVE:
      =================================================
      log4j2.xml
      =================================================
      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration monitorInterval="2" status="TRACE">
      <Appenders>
      <RollingFile name="rollingFileSync" fileName="logs/rolling.log" filePattern="logs/rolling.log.%i.gz">
      <PatternLayout pattern="%d %-5level %c

      {1.}

      - %msg%n" />
      <SizeBasedTriggeringPolicy size="100 KB" />
      <DefaultRolloverStrategy max="10" />
      </RollingFile>
      <Async name="rollingFileAsync" blocking="false">
      <AppenderRef ref="rollingFileSync" />
      </Async>
      </Appenders>
      <Loggers>
      <Root level="TRACE">
      <AppenderRef ref="rollingFileAsync" />
      </Root>
      </Loggers>
      </Configuration>
      =================================================
      code:
      public static void main(String[] args) {
      System.setProperty("log4j.configurationFile", "log4j2.xml");
      final Logger logger = LogManager.getLogger(RollingFileWithXmlConfigurationTest.class);
      int numRecords = 10 * 1000* 1000;
      for (int i = 0; i < numRecords; i++)

      { logger.log(Level.INFO, "The time is: " + System.nanoTime()); }

      }
      =================================================
      Here is a lead fixing the MCVE:
      $ git diff
      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 7981c7cc0..35f3cb1fb 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
      @@ -105,7 +105,7 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy {
      File file = new File(path);
      File parent = file.getParentFile();
      parent.mkdirs();

      • if (!path.contains("--1")) {
        + if (!path.contains("-1")) { return eligibleFiles; }

        Path dir = parent.toPath();
        @@ -114,7 +114,7 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy {
        if (suffixLength > 0)

        { fileName = fileName.substring(0, fileName.length() - suffixLength) + ".*"; }
      • String filePattern = fileName.replace("-1", "(
        d+)");
        + String filePattern = fileName.replace("-1", "(
        d+)");
        Pattern pattern = Pattern.compile(filePattern);

      try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                hzshlomi Shlomi Hazan
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - 24h
                  24h
                  Remaining:
                  Remaining Estimate - 24h
                  24h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified