Skip to content

Commit bc1dd7e

Browse files
committed
Resolve Github comments
1 parent e052c38 commit bc1dd7e

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.logging.log4j.core.appender.rolling;
1818

1919
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotEquals;
2120
import static org.junit.Assert.assertNotNull;
2221
import static org.junit.Assert.assertNull;
2322
import static org.junit.Assert.assertTrue;
@@ -143,21 +142,18 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
143142
assertNotNull(manager);
144143
manager.initialize();
145144

146-
// Get the initialTime of this original log file
147-
final long initialTime = manager.getFileTime();
148-
149145
// Log something to ensure that the existing file size is > 0
150146
final String testContent = "Test";
151147
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
152148

153149
// Trigger rollover that will fail
154150
manager.rollover();
155151

156-
// If the rollover fails, then the size should not be reset
157-
assertNotEquals(0, manager.getFileSize());
152+
// If the rollover fails, then the log file should be unchanged
153+
assertEquals(file.getAbsolutePath(), manager.getFileName());
158154

159-
// The initialTime should not have changed
160-
assertEquals(initialTime, manager.getFileTime());
155+
// The logged content should be unchanged
156+
assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII));
161157
}
162158

163159
@Test
@@ -197,14 +193,13 @@ public void testCreateParentDir() {
197193
@Test
198194
@Issue("https://github.com/apache/logging-log4j2/issues/2592")
199195
public void testRolloverOfDeletedFile() throws IOException {
200-
final Configuration configuration = new NullConfiguration();
201196
final File file = File.createTempFile("testRolloverOfDeletedFile", "log");
202197
file.deleteOnExit();
203198
final String testContent = "Test";
204199
try (final OutputStream os =
205200
new ByteArrayOutputStream(); // use a dummy OutputStream so that the real file can be deleted
206201
final RollingFileManager manager = new RollingFileManager(
207-
configuration.getLoggerContext(),
202+
null,
208203
file.getAbsolutePath(),
209204
"testRolloverOfDeletedFile.log.%d{yyyy-MM-dd}",
210205
os,
@@ -215,7 +210,7 @@ public void testRolloverOfDeletedFile() throws IOException {
215210
OnStartupTriggeringPolicy.createPolicy(1),
216211
DefaultRolloverStrategy.newBuilder().build(),
217212
file.getName(),
218-
PatternLayout.createDefaultLayout(configuration),
213+
null,
219214
null,
220215
null,
221216
null,

log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,9 @@ public synchronized void rollover() {
506506

507507
if (rollover(rolloverStrategy)) {
508508
try {
509+
size = 0;
510+
initialTime = System.currentTimeMillis();
509511
createFileAfterRollover();
510-
final File file = new File(getFileName());
511-
size = file.length();
512-
try {
513-
final FileTime creationTime = (FileTime) Files.getAttribute(file.toPath(), "creationTime");
514-
initialTime = creationTime.toMillis();
515-
} catch (Exception ex) {
516-
LOGGER.warn("Unable to get current file time for {}", file);
517-
initialTime = System.currentTimeMillis();
518-
}
519512
} catch (final IOException e) {
520513
logError("Failed to create file after rollover", e);
521514
}
@@ -611,43 +604,43 @@ public RolloverStrategy getRolloverStrategy() {
611604

612605
private boolean rollover(final RolloverStrategy strategy) {
613606

614-
boolean releaseRequired = false;
607+
boolean outputStreamClosed = false;
615608
try {
616609
// Block until the asynchronous operation is completed.
617610
semaphore.acquire();
618-
releaseRequired = true;
619611
} catch (final InterruptedException e) {
620612
logError("Thread interrupted while attempting to check rollover", e);
621-
return false;
613+
return outputStreamClosed;
622614
}
623615

624-
boolean success = true;
616+
boolean asyncActionStarted = true;
625617

626618
try {
627619
final RolloverDescription descriptor = strategy.rollover(this);
628620
if (descriptor != null) {
629621
writeFooter();
630622
closeOutputStream();
623+
outputStreamClosed = true;
624+
boolean syncActionSuccess = true;
631625
if (descriptor.getSynchronous() != null) {
632626
LOGGER.debug("RollingFileManager executing synchronous {}", descriptor.getSynchronous());
633627
try {
634-
success = descriptor.getSynchronous().execute();
628+
syncActionSuccess = descriptor.getSynchronous().execute();
635629
} catch (final Exception ex) {
636-
success = false;
630+
syncActionSuccess = false;
637631
logError("Caught error in synchronous task", ex);
638632
}
639633
}
640634

641-
if (success && descriptor.getAsynchronous() != null) {
635+
if (syncActionSuccess && descriptor.getAsynchronous() != null) {
642636
LOGGER.debug("RollingFileManager executing async {}", descriptor.getAsynchronous());
643637
asyncExecutor.execute(new AsyncAction(descriptor.getAsynchronous(), this));
644-
releaseRequired = false;
638+
asyncActionStarted = false;
645639
}
646-
return true;
647640
}
648-
return false;
641+
return outputStreamClosed;
649642
} finally {
650-
if (releaseRequired) {
643+
if (asyncActionStarted) {
651644
semaphore.release();
652645
}
653646
}

0 commit comments

Comments
 (0)