Skip to content

Commit d1cc807

Browse files
committed
Round initialTime in RollingFileManager
Caching of filesystem timestamps (at least in ext4 on Linux) results in non-accurate creation timestamps. Thus, let's round it to the second. Rounding is also applied for the lastModified time, but I'm not sure whether it's needed there. Fixes #3068.
1 parent 2f79c39 commit d1cc807

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,15 @@ void testRolloverOfDeletedFile() throws IOException {
224224
}
225225
assertEquals(testContent, new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII));
226226
}
227+
228+
@Test
229+
@Issue("https://github.com/apache/logging-log4j2/issues/3068")
230+
void testInitialTimeRounded() {
231+
assertEquals(1755031147000L, RollingFileManager.roundMillis(1755031147000L));
232+
assertEquals(1755031147000L, RollingFileManager.roundMillis(1755031147123L));
233+
assertEquals(1755031147000L, RollingFileManager.roundMillis(1755031147499L));
234+
assertEquals(1755031148000L, RollingFileManager.roundMillis(1755031147500L));
235+
assertEquals(1755031148000L, RollingFileManager.roundMillis(1755031147999L));
236+
assertEquals(1755031148000L, RollingFileManager.roundMillis(1755031148000L));
237+
}
227238
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,24 @@ private static long initialFileTime(final File file) {
918918
final FileTime fileTime = attrs.creationTime();
919919
if (fileTime.compareTo(EPOCH) > 0) {
920920
LOGGER.debug("Returning file creation time for {}", file.getAbsolutePath());
921-
return fileTime.toMillis();
921+
922+
return roundMillis(fileTime.toMillis());
922923
}
923-
LOGGER.info("Unable to obtain file creation time for " + file.getAbsolutePath());
924+
LOGGER.info("Unable to obtain file creation time for {}", file.getAbsolutePath());
924925
} catch (final Exception ex) {
925-
LOGGER.info("Unable to calculate file creation time for " + file.getAbsolutePath() + ": "
926-
+ ex.getMessage());
926+
LOGGER.info(
927+
"Unable to calculate file creation time for {}: {}", file.getAbsolutePath(), ex.getMessage());
927928
}
928929
}
929-
return file.lastModified();
930+
931+
return roundMillis(file.lastModified());
932+
}
933+
934+
/**
935+
* @see <a href="https://github.com/apache/logging-log4j2/issues/3068">Issue #3068</a>
936+
*/
937+
static long roundMillis(long millis) {
938+
return Math.round(millis / 1000d) * 1000;
930939
}
931940

932941
private static class EmptyQueue extends ArrayBlockingQueue<Runnable> {

0 commit comments

Comments
 (0)