Skip to content

Commit 17d7dd0

Browse files
Add support for nanoseconds in written_ts
`Instant.now()` is only available since Java 8. Hence, the animal-sniffer was updated to allow Java 8. Nano-second precision will only be reached with JRE >9. Otherwise it mill still be milliseconds.
1 parent 0d03cd0 commit 17d7dd0

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/TimestampConverter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.sap.hcp.cf.log4j2.converter;
22

3+
import java.time.Instant;
4+
35
import org.apache.logging.log4j.core.LogEvent;
46
import org.apache.logging.log4j.core.config.plugins.Plugin;
57
import org.apache.logging.log4j.core.pattern.ConverterKeys;
68
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
79

810
@Plugin(name = "TimestampConverter", category = "Converter")
911
@ConverterKeys({ "tstamp" })
12+
/**
13+
* This is a simple {@link LogEventPatternConverter} implementation that prints
14+
* the timestamp as a long in nano second resolution. Note: nano second
15+
* precision is only available from Java 9 or newer. Java 8 will only have milli
16+
* seconds.
17+
*/
1018
public class TimestampConverter extends LogEventPatternConverter {
1119

1220
public static final String WORD = "tstamp";
@@ -21,7 +29,9 @@ public static TimestampConverter newInstance(final String[] options) {
2129

2230
@Override
2331
public void format(LogEvent event, StringBuilder toAppendTo) {
24-
toAppendTo.append(System.currentTimeMillis() * 1000000);
32+
Instant now = Instant.now();
33+
long timestamp = now.getEpochSecond() * 1_000_000_000L + now.getNano();
34+
toAppendTo.append(timestamp);
2535
}
2636

2737
}

cf-java-logging-support-logback/src/main/java/com/sap/hcp/cf/logback/converter/TimestampConverter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.sap.hcp.cf.logback.converter;
22

3+
import java.time.Instant;
4+
35
import ch.qos.logback.classic.pattern.ClassicConverter;
46
import ch.qos.logback.classic.spi.ILoggingEvent;
57

68
/**
7-
* This is a simple {@link ClassicConverter} implementation that print the
8-
* timestamp as a long in nano second resolution.
9+
* This is a simple {@link ClassicConverter} implementation that prints the
10+
* timestamp as a long in nano second resolution. Note: nano second precision is
11+
* only available from Java 9 or newer. Java 8 will only have milli seconds.
912
*/
1013
public class TimestampConverter extends ClassicConverter {
1114

@@ -14,7 +17,9 @@ public class TimestampConverter extends ClassicConverter {
1417
@Override
1518
public String convert(ILoggingEvent event) {
1619
StringBuilder appendTo = new StringBuilder();
17-
appendTo.append(System.currentTimeMillis() * 1000000);
20+
Instant now = Instant.now();
21+
long timestamp = now.getEpochSecond() * 1_000_000_000L + now.getNano();
22+
appendTo.append(timestamp);
1823
return appendTo.toString();
1924
}
2025

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<junit.version>4.13.1</junit.version>
178178
<mockito.version>1.9.5</mockito.version>
179179
<surefire.plugin.version>2.18.1</surefire.plugin.version>
180-
<animal.sniffer.version>1.18</animal.sniffer.version>
180+
<animal.sniffer.version>1.19</animal.sniffer.version>
181181
<exec.plugin.version>1.4.0</exec.plugin.version>
182182
<javadoc.plugin.version>3.1.1</javadoc.plugin.version>
183183
<gpg.plugin.version>1.6</gpg.plugin.version>
@@ -268,7 +268,7 @@
268268
<configuration>
269269
<signature>
270270
<groupId>org.codehaus.mojo.signature</groupId>
271-
<artifactId>java16</artifactId>
271+
<artifactId>java18</artifactId>
272272
<version>1.0</version>
273273
</signature>
274274
</configuration>

0 commit comments

Comments
 (0)