Skip to content

Commit

Permalink
Feat(SystemClock): Added PreciseClock implementation to the system cl…
Browse files Browse the repository at this point in the history
…ock class, for the benefit of Java 8 users.
  • Loading branch information
Jay Katariya authored and jaykataria1111 committed Dec 13, 2024
1 parent 0f48f60 commit b147ca3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/3217_precise_clock.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="3217" link="https://github.com/apache/logging-log4j2/pull/3217"/>
<description format="asciidoc">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.
</description>
</entry>

0 comments on commit b147ca3

Please sign in to comment.