diff --git a/jpos/src/main/java/org/jpos/util/DailyLogListener.java b/jpos/src/main/java/org/jpos/util/DailyLogListener.java index 415f9cb315..3c8fd70fc1 100644 --- a/jpos/src/main/java/org/jpos/util/DailyLogListener.java +++ b/jpos/src/main/java/org/jpos/util/DailyLogListener.java @@ -20,6 +20,7 @@ import org.jpos.core.Configuration; import org.jpos.core.ConfigurationException; +import org.jpos.core.annotation.Config; import java.io.*; import java.nio.file.Files; @@ -56,6 +57,10 @@ public class DailyLogListener extends RotateLogListener{ private static final int DEF_BUFFER_SIZE = 128*1024;//128 KB private static final String[] DEF_COMPRESSED_SUFFIX= {"",".gz",".zip"}; private static final Map COMPRESSION_FORMATS = new HashMap(3); + + @Config("max-depth-deletion") + private int maxDepthDeletion = DEF_MAXDEPTH; + static { COMPRESSION_FORMATS.put("none", NONE); COMPRESSION_FORMATS.put("gzip", GZIP); @@ -93,7 +98,7 @@ public void setConfiguration(Configuration cfg) throws ConfigurationException { setCompressionBufferSize(cfg.getInt("compression-buffer-size", DEF_BUFFER_SIZE)); - deleteRegex = cfg.get("delete-regex", defaultDeleteRegex()); + deleteRegex = cfg.get("delete-regex", defaultDeleteRegex()); setLastDate(fmt.format(new Date())); @@ -178,7 +183,7 @@ public void deleteOldLogs() throws IOException { long currentSystemTime = System.currentTimeMillis(); try { - Files.find(logBasePath, DEF_MAXDEPTH, + Files.find(logBasePath, maxDepthDeletion, (path, attributes) -> path.getFileName().toString().matches(deleteRegex) && attributes.isRegularFile() diff --git a/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java b/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java index b4ac150d80..53ea1f4654 100644 --- a/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java +++ b/jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java @@ -20,17 +20,24 @@ import static org.apache.commons.lang3.JavaVersion.JAVA_14; import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.jpos.util.LogFileTestUtils.getStringFromCompressedFile; import static org.jpos.util.LogFileTestUtils.getStringFromFile; import static org.junit.jupiter.api.Assertions.*; +import static org.hamcrest.io.FileMatchers.anExistingFile; +import static org.hamcrest.MatcherAssert.*; import java.io.*; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.FileTime; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.Properties; import java.util.zip.Deflater; @@ -40,6 +47,7 @@ import org.jpos.core.ConfigurationException; import org.jpos.core.SimpleConfiguration; import org.jpos.core.SubConfiguration; +import org.jpos.q2.QFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -427,6 +435,43 @@ public void testMaxAgeFeatureWhenThereIsNonLogFiles() throws Exception { listener.destroy(); } + @Test + public void testDeleteOldLogsWithCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); + Files.createDirectory(parent); + Path child = Files.createTempFile(parent, "child", ".log"); + assertThat("file should have been created", child.toFile(), is(anExistingFile())); + Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough + DailyLogListener listener = new DailyLogListener(); + SimpleConfiguration cfg = new SimpleConfiguration(); + cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString()); + cfg.put("max-depth-deletion", "2"); + cfg.put("delete-regex", "^child.*\\.log"); + cfg.put("maxage", "1000"); + QFactory.autoconfigure(listener, cfg); + listener.setConfiguration(cfg); + listener.deleteOldLogs(); + assertThat("file should have been deleted", child.toFile(), is(not(anExistingFile()))); + + } + @Test + public void testDeleteOldLogsWithoutCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException { + Path parent = logRotationTestDirectory.getDirectory().resolve("parent"); + Files.createDirectory(parent); + Path child = Files.createTempFile(parent, "child", ".log"); + assertThat("file should have been created", child.toFile(), is(anExistingFile())); + Files.setLastModifiedTime(child, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough + DailyLogListener listener = new DailyLogListener(); + SimpleConfiguration cfg = new SimpleConfiguration(); + cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString()); + cfg.put("delete-regex", "^child.*\\.log"); + cfg.put("maxage", "1000"); + QFactory.autoconfigure(listener, cfg); + listener.setConfiguration(cfg); + listener.deleteOldLogs(); + assertThat("file should have not been deleted", child.toFile(), is(anExistingFile())); + + } @Test @Disabled("This feature doesn't work in Windows so we reverted the patch c94ff02f2") public void testLogRotateAbortsWhenCreatingNewFileFails() throws Exception {