Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -17,13 +17,17 @@
package org.apache.logging.log4j.core.appender.rolling;

import static org.junit.Assert.assertEquals;
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 static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.logging.log4j.core.LoggerContext;
Expand Down Expand Up @@ -185,4 +189,40 @@ public void testCreateParentDir() {
manager.close();
}
}

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/2592")
public void testRolloverOfDeletedFile() throws IOException {
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(
null,
null,
file.getAbsolutePath(),
"testRolloverOfDeletedFile.log.%d{yyyy-MM-dd}",
os,
true,
false,
0,
System.currentTimeMillis(),
OnStartupTriggeringPolicy.createPolicy(1),
DefaultRolloverStrategy.newBuilder().build(),
file.getName(),
null,
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, Files.readString(file.toPath(), StandardCharsets.US_ASCII));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,43 +516,43 @@ public RolloverStrategy getRolloverStrategy() {

private boolean rollover(final RolloverStrategy strategy) {

boolean releaseRequired = false;
boolean outputStreamClosed = false;
try {
// Block until the asynchronous operation is completed.
semaphore.acquire();
releaseRequired = true;
} catch (final InterruptedException e) {
logError("Thread interrupted while attempting to check rollover", e);
return false;
return outputStreamClosed;
}

boolean success = true;
boolean asyncActionStarted = true;

try {
final RolloverDescription descriptor = strategy.rollover(this);
if (descriptor != null) {
writeFooter();
closeOutputStream();
outputStreamClosed = true;
boolean syncActionSuccess = true;
if (descriptor.getSynchronous() != null) {
LOGGER.debug("RollingFileManager executing synchronous {}", descriptor.getSynchronous());
try {
success = descriptor.getSynchronous().execute();
syncActionSuccess = descriptor.getSynchronous().execute();
} catch (final Exception ex) {
success = false;
syncActionSuccess = false;
logError("Caught error in synchronous task", ex);
}
}

if (success && descriptor.getAsynchronous() != null) {
if (syncActionSuccess && descriptor.getAsynchronous() != null) {
LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
releaseRequired = false;
asyncActionStarted = false;
}
return success;
}
return false;
return outputStreamClosed;
} finally {
if (releaseRequired) {
if (asyncActionStarted) {
semaphore.release();
}
}
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changelog entry is not needed, please remove.

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>
Loading