Skip to content

Commit

Permalink
Let the max depth for deletion be configurable.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrés Alcarraz <[email protected]>

jpos#580 About the rotation of q2 logs
  • Loading branch information
alcarraz committed Feb 9, 2024
1 parent 98ae51d commit 91d9899
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
9 changes: 7 additions & 2 deletions jpos/src/main/java/org/jpos/util/DailyLogListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String,Integer> COMPRESSION_FORMATS = new HashMap<String,Integer>(3);

@Config("max-depth-deletion")
private int maxDepthDeletion = DEF_MAXDEPTH;

static {
COMPRESSION_FORMATS.put("none", NONE);
COMPRESSION_FORMATS.put("gzip", GZIP);
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions jpos/src/test/java/org/jpos/util/DailyLogListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 91d9899

Please sign in to comment.