Skip to content

Commit 3b71a00

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 3b71a00

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,19 @@ 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+
// See https://github.com/apache/logging-log4j2/issues/3068
923+
return Math.round(fileTime.toMillis() / 1000d) * 1000;
922924
}
923-
LOGGER.info("Unable to obtain file creation time for " + file.getAbsolutePath());
925+
LOGGER.info("Unable to obtain file creation time for {}", file.getAbsolutePath());
924926
} catch (final Exception ex) {
925-
LOGGER.info("Unable to calculate file creation time for " + file.getAbsolutePath() + ": "
926-
+ ex.getMessage());
927+
LOGGER.info(
928+
"Unable to calculate file creation time for {}: {}", file.getAbsolutePath(), ex.getMessage());
927929
}
928930
}
929-
return file.lastModified();
931+
932+
// See https://github.com/apache/logging-log4j2/issues/3068
933+
return Math.round(file.lastModified() / 1000d) * 1000;
930934
}
931935

932936
private static class EmptyQueue extends ArrayBlockingQueue<Runnable> {

0 commit comments

Comments
 (0)