From b147ca3301c36d7c1d79ecc63ad91219c2c6f52f Mon Sep 17 00:00:00 2001 From: Jay Katariya Date: Tue, 19 Nov 2024 00:16:06 -0800 Subject: [PATCH] Feat(SystemClock): Added PreciseClock implementation to the system clock class, for the benefit of Java 8 users. --- .../logging/log4j/core/util/SystemClock.java | 16 +++++++++++++++- .../logging/log4j/core/util/package-info.java | 2 +- src/changelog/.2.x.x/3217_precise_clock.xml | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/changelog/.2.x.x/3217_precise_clock.xml diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java index e1a9cd2a09a..1094ce8c04d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java @@ -16,10 +16,15 @@ */ package org.apache.logging.log4j.core.util; +import java.time.Instant; +import org.apache.logging.log4j.core.time.MutableInstant; +import org.apache.logging.log4j.core.time.PreciseClock; + /** * Implementation of the {@code Clock} interface that returns the system time. + * @since 2.25 */ -public final class SystemClock implements Clock { +public final class SystemClock implements Clock, PreciseClock { /** * Returns the system time. @@ -29,4 +34,13 @@ public final class SystemClock implements Clock { public long currentTimeMillis() { return System.currentTimeMillis(); } + + /** + * {@inheritDoc} + */ + @Override + public void init(final MutableInstant mutableInstant) { + final Instant instant = java.time.Clock.systemUTC().instant(); + mutableInstant.initFromEpochSecond(instant.getEpochSecond(), instant.getNano()); + } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/package-info.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/package-info.java index 06e847dcfef..932e8bc0800 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/package-info.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/package-info.java @@ -18,7 +18,7 @@ * Log4j 2 helper classes. */ @Export -@Version("2.24.2") +@Version("2.25.0") package org.apache.logging.log4j.core.util; import org.osgi.annotation.bundle.Export; diff --git a/src/changelog/.2.x.x/3217_precise_clock.xml b/src/changelog/.2.x.x/3217_precise_clock.xml new file mode 100644 index 00000000000..e35dc3f1682 --- /dev/null +++ b/src/changelog/.2.x.x/3217_precise_clock.xml @@ -0,0 +1,10 @@ + + + + Currently Java 8 users do not have access to timestamps with sub-millisecond precision. + This change moves the SystemClock implementation from log4j-core-java9 to log4j-core, overriding the current one, so that java 8 users can benefit from it. + +