Details
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
- %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++)
}
=================================================
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();
{ fileName = fileName.substring(0, fileName.length() - suffixLength) + ".*"; }
@@ -114,7 +114,7 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy {
if (suffixLength > 0) - 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
- duplicates
-
LOG4J2-1804 Rolling file %i based rolling broken in 2.8
- Closed