Skip to content

Commit 503ed08

Browse files
committed
Let the max depth for deletion be configurable.
Signed-off-by: Andrés Alcarraz <alcarraz@gmail.com> #580 About the rotation of q2 logs
1 parent 98ae51d commit 503ed08

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

jpos/src/main/java/org/jpos/util/DailyLogListener.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.jpos.core.Configuration;
2222
import org.jpos.core.ConfigurationException;
23+
import org.jpos.core.annotation.Config;
2324

2425
import java.io.*;
2526
import java.nio.file.Files;
@@ -56,6 +57,10 @@ public class DailyLogListener extends RotateLogListener{
5657
private static final int DEF_BUFFER_SIZE = 128*1024;//128 KB
5758
private static final String[] DEF_COMPRESSED_SUFFIX= {"",".gz",".zip"};
5859
private static final Map<String,Integer> COMPRESSION_FORMATS = new HashMap<String,Integer>(3);
60+
61+
@Config("max-depth-deletion")
62+
private int maxDepthDeletion = DEF_MAXDEPTH;
63+
5964
static {
6065
COMPRESSION_FORMATS.put("none", NONE);
6166
COMPRESSION_FORMATS.put("gzip", GZIP);
@@ -93,7 +98,7 @@ public void setConfiguration(Configuration cfg) throws ConfigurationException {
9398
setCompressionBufferSize(cfg.getInt("compression-buffer-size",
9499
DEF_BUFFER_SIZE));
95100

96-
deleteRegex = cfg.get("delete-regex", defaultDeleteRegex());
101+
deleteRegex = cfg.get("delete-regex", defaultDeleteRegex());
97102

98103
setLastDate(fmt.format(new Date()));
99104

@@ -178,7 +183,7 @@ public void deleteOldLogs() throws IOException {
178183
long currentSystemTime = System.currentTimeMillis();
179184

180185
try {
181-
Files.find(logBasePath, DEF_MAXDEPTH,
186+
Files.find(logBasePath, maxDepthDeletion,
182187
(path, attributes) ->
183188
path.getFileName().toString().matches(deleteRegex)
184189
&& attributes.isRegularFile()

jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java

+68
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@
2020

2121
import static org.apache.commons.lang3.JavaVersion.JAVA_14;
2222
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
23+
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.not;
2325
import static org.jpos.util.LogFileTestUtils.getStringFromCompressedFile;
2426
import static org.jpos.util.LogFileTestUtils.getStringFromFile;
2527
import static org.junit.jupiter.api.Assertions.*;
28+
import static org.hamcrest.io.FileMatchers.anExistingFile;
29+
import static org.hamcrest.MatcherAssert.*;
2630

2731
import java.io.*;
2832
import java.nio.file.Files;
2933
import java.nio.file.LinkOption;
3034
import java.nio.file.Path;
3135
import java.nio.file.Paths;
36+
import java.nio.file.attribute.FileTime;
3237
import java.text.DateFormat;
3338
import java.text.SimpleDateFormat;
39+
import java.time.Instant;
40+
import java.time.temporal.ChronoUnit;
3441
import java.util.Date;
3542
import java.util.Properties;
3643
import java.util.zip.Deflater;
@@ -40,6 +47,8 @@
4047
import org.jpos.core.ConfigurationException;
4148
import org.jpos.core.SimpleConfiguration;
4249
import org.jpos.core.SubConfiguration;
50+
import org.jpos.q2.QFactory;
51+
import org.junit.jupiter.api.AfterEach;
4352
import org.junit.jupiter.api.BeforeEach;
4453
import org.junit.jupiter.api.Disabled;
4554
import org.junit.jupiter.api.Test;
@@ -427,6 +436,65 @@ public void testMaxAgeFeatureWhenThereIsNonLogFiles() throws Exception {
427436
listener.destroy();
428437
}
429438

439+
@Test
440+
public void testDeleteOldLogsWithCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException {
441+
Path parent = logRotationTestDirectory.getDirectory().resolve("parent");
442+
//create a file at level 0 in condition to be deleted.
443+
Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log");
444+
assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile()));
445+
Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
446+
//create a file at level 1 in condition to be deleted.
447+
Files.createDirectory(parent);
448+
Path level1file = Files.createTempFile(parent, "child", ".log");
449+
assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile()));
450+
Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
451+
452+
try (DailyLogListener listener = new DailyLogListener()) {
453+
SimpleConfiguration cfg = new SimpleConfiguration();
454+
cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString());
455+
cfg.put("max-depth-deletion", "2");
456+
cfg.put("delete-regex", "^child.*\\.log");
457+
cfg.put("maxage", "1"); //created files are much older than 1s
458+
QFactory.autoconfigure(listener, cfg);
459+
listener.setConfiguration(cfg);
460+
461+
listener.deleteOldLogs();
462+
}
463+
assertThat("level 1 file should have been deleted", level1file.toFile(), is(not(anExistingFile())));
464+
assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile())));
465+
Files.delete(parent);
466+
467+
}
468+
@Test
469+
public void testDeleteOldLogsWithoutCustomMaxDepth() throws ConfigurationException, IOException, IllegalAccessException {
470+
Path parent = logRotationTestDirectory.getDirectory().resolve("parent");
471+
//create a file at level 0 in condition to be deleted.
472+
Path level0file = Files.createTempFile(logRotationTestDirectory.getDirectory(), "child", ".log");
473+
assertThat("level 0 file should have been created", level0file.toFile(), is(anExistingFile()));
474+
Files.setLastModifiedTime(level0file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
475+
//create a file at level 1 in condition to be deleted.
476+
Files.createDirectory(parent);
477+
Path level1file = Files.createTempFile(parent, "child", ".log");
478+
assertThat("level 1 file should have been created", level1file.toFile(), is(anExistingFile()));
479+
Files.setLastModifiedTime(level1file, FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS))); //old enough
480+
481+
try (DailyLogListener listener = new DailyLogListener()) {
482+
SimpleConfiguration cfg = new SimpleConfiguration();
483+
cfg.put("prefix", logRotationTestDirectory.getFile("q2").toString());
484+
cfg.put("delete-regex", "^child.*\\.log");
485+
cfg.put("maxage", "1000");
486+
QFactory.autoconfigure(listener, cfg);
487+
listener.setConfiguration(cfg);
488+
489+
listener.deleteOldLogs();
490+
}
491+
assertThat("level 1 file should have not been deleted", level1file.toFile(), is(anExistingFile()));
492+
assertThat("level 0 file should have been deleted", level0file.toFile(), is(not(anExistingFile())));
493+
494+
Files.delete(level1file); //so it doesn't give problems in windows
495+
Files.delete(parent);
496+
497+
}
430498
@Test
431499
@Disabled("This feature doesn't work in Windows so we reverted the patch c94ff02f2")
432500
public void testLogRotateAbortsWhenCreatingNewFileFails() throws Exception {

0 commit comments

Comments
 (0)