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 7981c7c..88cd2ac 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 @@ -22,10 +22,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Objects; import java.util.SortedMap; import java.util.TreeMap; import java.util.regex.Matcher; @@ -34,10 +31,7 @@ import java.util.regex.Pattern; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LoggingException; import org.apache.logging.log4j.core.appender.rolling.action.Action; -import org.apache.logging.log4j.core.appender.rolling.action.CommonsCompressAction; import org.apache.logging.log4j.core.appender.rolling.action.CompositeAction; -import org.apache.logging.log4j.core.appender.rolling.action.GzCompressAction; -import org.apache.logging.log4j.core.appender.rolling.action.ZipCompressAction; import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.apache.logging.log4j.status.StatusLogger; @@ -92,20 +86,21 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy { protected SortedMap getEligibleFiles(final RollingFileManager manager, final boolean isAscending) { final StringBuilder buf = new StringBuilder(); - manager.getPatternProcessor().formatFileName(strSubstitutor, buf, -1); - return getEligibleFiles(buf.toString(), isAscending); + manager.getPatternProcessor().formatFileName(strSubstitutor, buf, Integer.MAX_VALUE); + return getEligibleFiles(buf.toString(), manager.getPatternProcessor().getPattern(), isAscending); } - protected SortedMap getEligibleFiles(String path) { - return getEligibleFiles(path, true); + protected SortedMap getEligibleFiles(String path, String logFilePattern) { + return getEligibleFiles(path, logFilePattern, true); } - protected SortedMap getEligibleFiles(String path, boolean isAscending) { + protected SortedMap getEligibleFiles(String path, String logFilePattern, + boolean isAscending) { TreeMap eligibleFiles = new TreeMap<>(); File file = new File(path); File parent = file.getParentFile(); parent.mkdirs(); - if (!path.contains("--1")) { + if (!logFilePattern.contains("%i")) { return eligibleFiles; } Path dir = parent.toPath(); @@ -114,7 +109,8 @@ public abstract class AbstractRolloverStrategy implements RolloverStrategy { if (suffixLength > 0) { fileName = fileName.substring(0, fileName.length() - suffixLength) + ".*"; } - String filePattern = fileName.replace("--1", "-(\\d+)"); + // TODO: It should be done using the logFilePattern, not arbitrary value + String filePattern = fileName.replace(Integer.toString(Integer.MAX_VALUE), "(\\d+)"); Pattern pattern = Pattern.compile(filePattern); try (DirectoryStream stream = Files.newDirectoryStream(dir)) { 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 3f29b2d..9d58141 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 @@ -30,7 +30,8 @@ public class EligibleFilesTest { @Test public void runTest() throws Exception { - String path = "target/test-classes/rolloverPath/log4j.txt.20170112_09--1.gz"; + String path = "target/test-classes/rolloverPath/log4j.txt.20170112_09-" + + Integer.toString(Integer.MAX_VALUE) + ".gz"; TestRolloverStrategy strategy = new TestRolloverStrategy(); Map files = strategy.findFilesInPath(path); assertTrue("No files found", files.size() > 0); @@ -49,7 +50,7 @@ public class EligibleFilesTest { } public Map findFilesInPath(String path) { - return getEligibleFiles(path); + return getEligibleFiles(path, "log4j.txt.%d{yyyyMMdd}-%i.gz"); } } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeNoCompressTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeNoCompressTest.java new file mode 100644 index 0000000..a2944c9 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeNoCompressTest.java @@ -0,0 +1,97 @@ +/* + * 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. + */ +package org.apache.logging.log4j.core.appender.rolling; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.compress.utils.IOUtils; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; + +/** + * LOG4J2-1804. + */ +public class RollingAppenderSizeNoCompressTest { + + private static final String CONFIG = "log4j-rolling-size.xml"; + + private static final String DIR = "target/rolling1"; + + public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG); + + @Rule + public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR); + + private Logger logger; + + @Before + public void setUp() throws Exception { + this.logger = loggerContextRule.getLogger(RollingAppenderSizeNoCompressTest.class.getName()); + } + + @Test + public void testAppender() throws Exception { + List messages = new ArrayList<>(); + for (int i=0; i < 200; ++i) { + String message = "This is test message number " + i; + messages.add(message); + logger.debug(message); + if (i % 50 == 0) { + Thread.sleep(500); + } + } + if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { + System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this); + } + final File dir = new File(DIR); + assertTrue("Directory not created", dir.exists()); + final File[] files = dir.listFiles(); + assertNotNull(files); + for (File file : files) { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (FileInputStream fis = new FileInputStream(file)) { + try { + IOUtils.copy(fis, baos); + } catch (final Exception ex) { + ex.printStackTrace(); + fail("Unable to read " + file.getAbsolutePath()); + } + } + final String text = new String(baos.toByteArray(), Charset.defaultCharset()); + final String[] lines = text.split("[\\r\\n]+"); + for (final String line : lines) { + messages.remove(line); + } + } + assertTrue("Log messages lost : " + messages.size(), messages.isEmpty()); + assertTrue("Files not rolled : " + files.length, files.length > 2); + } +} diff --git a/log4j-core/src/test/resources/log4j-rolling-size.xml b/log4j-core/src/test/resources/log4j-rolling-size.xml new file mode 100644 index 0000000..9760f40 --- /dev/null +++ b/log4j-core/src/test/resources/log4j-rolling-size.xml @@ -0,0 +1,58 @@ + + + + + target/rolling1/rollingtest.log + + + + + + + + + + %m%n + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mvnw.cmd b/mvnw.cmd index 181675f..dcb298e 100644 --- a/mvnw.cmd +++ b/mvnw.cmd @@ -118,7 +118,7 @@ for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do s SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" -set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_JAR=""%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"" set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain @REM avoid using MAVEN_CMD_LINE_ARGS below since that would lose parameter escaping in %*