Skip to content

Commit

Permalink
Support continuing log files even if there are other newer files
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwmedia committed Jan 9, 2024
1 parent 87782d4 commit f200377
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public RollingFileWriter(final Map<String, String> properties) throws IOExceptio
linkToLatest = properties.containsKey("latest") ? new DynamicPath(getStringValue("latest")) : null;

List<FileTuple> files = getAllFileTuplesWithoutLinks(converter.getBackupSuffix());
File latestFile = findLatestLogFile(files);
File latestFile = findLatestValidLogFile(path, files);

if (backups >= 0) {
deleteBackups(files, backups);
Expand All @@ -93,7 +93,7 @@ public RollingFileWriter(final Map<String, String> properties) throws IOExceptio
String fileName;
boolean append;

if (latestFile != null && path.isValid(latestFile)) {
if (latestFile != null) {
fileName = latestFile.getAbsolutePath();
if (canBeContinued(fileName, policies)) {
append = true;
Expand Down Expand Up @@ -262,16 +262,21 @@ private ByteArrayWriter createByteArrayWriterAndLinkLatest(final String fileName
}

/**
* Finds the latest existing original log file.
* Finds the latest valid existing original log file.
*
* @param path
* Dynamic path instance fo validation
* @param files
* All original and backup files
* @return Found original log file or {@code null} if there are no original log files
*/
private static File findLatestLogFile(final List<FileTuple> files) {
private static File findLatestValidLogFile(final DynamicPath path, final List<FileTuple> files) {
for (FileTuple file : files) {
if (file.getOriginal().isFile() && (file.getOriginal().equals(file.getBackup()) || !file.getBackup().isFile())) {
return file.getOriginal();
File original = file.getOriginal();
if (path.isValid(original)) {
return original;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -27,6 +28,7 @@
import org.tinylog.converters.FileConverter;
import org.tinylog.core.LogEntryValue;
import org.tinylog.path.DynamicSegment;
import org.tinylog.policies.DynamicPolicy;
import org.tinylog.rules.SystemStreamCollector;
import org.tinylog.util.FileSystem;
import org.tinylog.util.LogEntryBuilder;
Expand Down Expand Up @@ -61,6 +63,16 @@ public final class RollingFileWriterTest {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

/**
* Resets the static dynamic segment and policy fields.
*/
@After
public void reset() {
Whitebox.setInternalState(DynamicSegment.class, boolean.class, false, DynamicSegment.class);
Whitebox.setInternalState(DynamicSegment.class, String.class, null, DynamicSegment.class);
Whitebox.setInternalState(DynamicPolicy.class, boolean.class, false, DynamicPolicy.class);
}

/**
* Verifies that log entries will be immediately output, if buffer is disabled.
*
Expand Down Expand Up @@ -202,7 +214,7 @@ public void defaultFormatPattern() throws IOException, InterruptedException {
* Interrupted while waiting for the converter
*/
@Test
public void continueExistingFile() throws IOException, InterruptedException {
public void continueSingleExistingFile() throws IOException, InterruptedException {
String file = FileSystem.createTemporaryFile();
Map<String, String> properties = tripletonMap("file", file, "format", "{message}", "policies", "size: 1MB");

Expand All @@ -217,6 +229,34 @@ public void continueExistingFile() throws IOException, InterruptedException {
assertThat(FileSystem.readFile(file)).isEqualTo("First" + NEW_LINE + "Second" + NEW_LINE);
}

/**
* Verifies that the latest valid existing file can be continued.
*
* @throws IOException
* Failed access to temporary file
* @throws InterruptedException
* Interrupted while waiting for the converter
*/
@Test
public void continueLatestValidExistingFile() throws IOException, InterruptedException {
File file1 = folder.newFile("a");
File file2 = folder.newFile("42");
File file3 = folder.newFile("b");

file1.setLastModified(0);
file2.setLastModified(1000);
file3.setLastModified(2000);

String file = new File(folder.getRoot(), "{count}").getAbsolutePath();
Map<String, String> properties = tripletonMap("file", file, "format", "{message}", "policies", "size: 1MB");

RollingFileWriter writer = new RollingFileWriter(properties);
writer.write(LogEntryBuilder.empty().message("Hello World!").create());
writer.close();

assertThat(FileSystem.readFile(file2.getPath())).isEqualTo("Hello World!" + NEW_LINE);
}

/**
* Verifies that a new file can be started.
*
Expand Down Expand Up @@ -522,19 +562,20 @@ public void isRegistered() throws IOException {
public void dynamicText() throws IOException, InterruptedException {
String fooFile = FileSystem.createTemporaryFile();
String barFile = FileSystem.createTemporaryFile();
String bazFile = FileSystem.createTemporaryFile();

RollingFileWriter writer = new RollingFileWriter(tripletonMap(
"file", "{dynamic}",
"file", "{dynamic:" + fooFile + "}",
"format", "{message}",
"policies", "dynamic"));
DynamicSegment.setText(fooFile);
writer.write(LogEntryBuilder.empty().message("Hello World!").create());
DynamicSegment.setText(barFile);
writer.write(LogEntryBuilder.empty().message("Hello World!").create());
DynamicSegment.setText(bazFile);
writer.write(LogEntryBuilder.empty().message("Goodbye!").create());
writer.close();

assertThat(FileSystem.readFile(fooFile)).isEqualTo("Hello World!" + NEW_LINE);
assertThat(FileSystem.readFile(barFile)).isEqualTo("Goodbye!" + NEW_LINE);
assertThat(FileSystem.readFile(barFile)).isEqualTo("Hello World!" + NEW_LINE);
assertThat(FileSystem.readFile(bazFile)).isEqualTo("Goodbye!" + NEW_LINE);
}

/**
Expand Down

0 comments on commit f200377

Please sign in to comment.