This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 187f38cd89 Round `initialTime` in `RollingFileManager` (#3872)
187f38cd89 is described below
commit 187f38cd897b0f5a16fbfc68f5860935f4a7fe23
Author: Niklas Keller <[email protected]>
AuthorDate: Mon Sep 1 10:40:11 2025 +0200
Round `initialTime` in `RollingFileManager` (#3872)
Co-authored-by: Volkan Yazıcı <[email protected]>
---
.../appender/rolling/RollingFileManagerTest.java | 16 ++++++++++++++++
.../core/appender/rolling/RollingFileManager.java | 21 +++++++++++++++------
.../3872_fix_RollingFileManager_initialTime.xml | 13 +++++++++++++
3 files changed, 44 insertions(+), 6 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java
index 78784cfa75..6497d3a2f2 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManagerTest.java
@@ -224,4 +224,20 @@ class RollingFileManagerTest {
}
assertEquals(testContent, new
String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII));
}
+
+ @Test
+ @Issue("https://github.com/apache/logging-log4j2/issues/3068")
+ void testInitialTimeRounded() throws IOException {
+ assertEquals(1755031147000L,
RollingFileManager.alignMillisToSecond(1755031147000L));
+ assertEquals(1755031147000L,
RollingFileManager.alignMillisToSecond(1755031147123L));
+ assertEquals(1755031147000L,
RollingFileManager.alignMillisToSecond(1755031147499L));
+ assertEquals(1755031148000L,
RollingFileManager.alignMillisToSecond(1755031147500L));
+ assertEquals(1755031148000L,
RollingFileManager.alignMillisToSecond(1755031147999L));
+ assertEquals(1755031148000L,
RollingFileManager.alignMillisToSecond(1755031148000L));
+
+ final File file = File.createTempFile("testFile", "log");
+ file.deleteOnExit();
+
+ assertEquals(0, RollingFileManager.initialFileTime(file) % 1000);
+ }
}
diff --git
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
index b033f5785e..7aec034036 100644
---
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
+++
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.java
@@ -910,7 +910,7 @@ public class RollingFileManager extends FileManager {
}
}
- private static long initialFileTime(final File file) {
+ static long initialFileTime(final File file) {
final Path path = file.toPath();
if (Files.exists(path)) {
try {
@@ -918,15 +918,24 @@ public class RollingFileManager extends FileManager {
final FileTime fileTime = attrs.creationTime();
if (fileTime.compareTo(EPOCH) > 0) {
LOGGER.debug("Returning file creation time for {}",
file.getAbsolutePath());
- return fileTime.toMillis();
+
+ return alignMillisToSecond(fileTime.toMillis());
}
- LOGGER.info("Unable to obtain file creation time for " +
file.getAbsolutePath());
+ LOGGER.info("Unable to obtain file creation time for {}",
file.getAbsolutePath());
} catch (final Exception ex) {
- LOGGER.info("Unable to calculate file creation time for " +
file.getAbsolutePath() + ": "
- + ex.getMessage());
+ LOGGER.info(
+ "Unable to calculate file creation time for {}: {}",
file.getAbsolutePath(), ex.getMessage());
}
}
- return file.lastModified();
+
+ return alignMillisToSecond(file.lastModified());
+ }
+
+ /**
+ * @see <a
href="https://github.com/apache/logging-log4j2/issues/3068">Issue #3068</a>
+ */
+ static long alignMillisToSecond(long millis) {
+ return Math.round(millis / 1000d) * 1000;
}
private static class EmptyQueue extends ArrayBlockingQueue<Runnable> {
diff --git a/src/changelog/.2.x.x/3872_fix_RollingFileManager_initialTime.xml
b/src/changelog/.2.x.x/3872_fix_RollingFileManager_initialTime.xml
new file mode 100644
index 0000000000..ba6fbd9129
--- /dev/null
+++ b/src/changelog/.2.x.x/3872_fix_RollingFileManager_initialTime.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ https://logging.apache.org/xml/ns
+ https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="3068"
link="https://github.com/apache/logging-log4j2/issues/3068"/>
+ <issue id="3872"
link="https://github.com/apache/logging-log4j2/pull/3872"/>
+ <description format="asciidoc">
+ Discard the sub-second part while obtaining the initial time (i.e.,
creation time) of a file in `RollingFileManager`
+ </description>
+</entry>