Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
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.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
Expand Down Expand Up @@ -188,4 +193,40 @@ public void testCreateParentDir() {
manager.close();
}
}

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/2592")
public void testRolloverOfDeletedFile() throws IOException {
final Configuration configuration = new NullConfiguration();
final File file = File.createTempFile("testRolloverOfDeletedFile", "log");
file.deleteOnExit();
final String testContent = "Test";
try (final OutputStream os =
new ByteArrayOutputStream(); // use a dummy OutputStream so that the real file can be deleted
final RollingFileManager manager = new RollingFileManager(
configuration.getLoggerContext(),
file.getAbsolutePath(),
"testRolloverOfDeletedFile.log.%d{yyyy-MM-dd}",
os,
true,
false,
0,
System.currentTimeMillis(),
OnStartupTriggeringPolicy.createPolicy(1),
DefaultRolloverStrategy.newBuilder().build(),
file.getName(),
PatternLayout.createDefaultLayout(configuration),
null,
null,
null,
false,
ByteBuffer.allocate(256))) {
assertTrue(file.delete());
manager.setRenameEmptyFiles(true);
manager.rollover();
assertEquals(file.getAbsolutePath(), manager.getFileName());
manager.writeBytes(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
}
assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,16 @@ public synchronized void rollover() {

if (rollover(rolloverStrategy)) {
try {
size = 0;
initialTime = System.currentTimeMillis();
createFileAfterRollover();
final File file = new File(getFileName());
size = file.length();
try {
final FileTime creationTime = (FileTime) Files.getAttribute(file.toPath(), "creationTime");
initialTime = creationTime.toMillis();
} catch (Exception ex) {
LOGGER.warn("Unable to get current file time for {}", file);
initialTime = System.currentTimeMillis();
}
} catch (final IOException e) {
logError("Failed to create file after rollover", e);
}
Expand Down Expand Up @@ -636,7 +643,7 @@ private boolean rollover(final RolloverStrategy strategy) {
asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
releaseRequired = false;
}
return success;
return true;
}
return false;
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="2592" link="https://github.com/apache/logging-log4j2/issues/2592"/>
<description format="asciidoc">Fix `RollingFileManager` to reopen the log file when the rollover was unsuccessful</description>
</entry>