[
https://issues.apache.org/jira/browse/LOG4J2-3075?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17341884#comment-17341884
]
Paul Rubin edited comment on LOG4J2-3075 at 5/10/21, 12:40 PM:
---------------------------------------------------------------
I do not know if this will help or not I just created a String lookup that
internally uses a DateTimeFormatter:
{code:java}
@Plugin(name = "timestampnano", category = StrLookup.CATEGORY)
public class TimestampNano implements StrLookup {
private String getTimeInNano() {
Instant now = Instant.now();
return getTimestampNano(now);
}
private String getTimestampNano(Instant now) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
return formatter.format(now);
}
@Override
public String lookup(String key) {
return getTimeInNano();
}
@Override
public String lookup(LogEvent event, String key) {
Instant now = Instant.now();
if (!key.isEmpty()) {
now = Instant.ofEpochSecond(event.getInstant().getEpochSecond(),
event.getInstant().getNanoOfSecond());
}
return getTimestampNano(now);
}
}
{code}
I likely misused the key in the lookup, but I was trying to provide the ability
to get the current time instead of the event time just so I could see the
difference.
was (Author: paulrubin37):
I do not know if this will help or not I just create a String lookup that
internally uses a DateTimeFormatter:
{code:java}
@Plugin(name = "timestampnano", category = StrLookup.CATEGORY)
public class TimestampNano implements StrLookup {
private String getTimeInNano() {
Instant now = Instant.now();
return getTimestampNano(now);
}
private String getTimestampNano(Instant now) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
return formatter.format(now);
}
@Override
public String lookup(String key) {
return getTimeInNano();
}
@Override
public String lookup(LogEvent event, String key) {
Instant now = Instant.now();
if (!key.isEmpty()) {
now = Instant.ofEpochSecond(event.getInstant().getEpochSecond(),
event.getInstant().getNanoOfSecond());
}
return getTimestampNano(now);
}
}
{code}
I likely misused the key in the lookup, but I was trying to provide the ability
to get the current time instead of the event time just so I could see the
difference.
> JsonTemplateLayout timestamp resolver doesn't format nanoseconds
> ----------------------------------------------------------------
>
> Key: LOG4J2-3075
> URL: https://issues.apache.org/jira/browse/LOG4J2-3075
> Project: Log4j 2
> Issue Type: Bug
> Components: JsonTemplateLayout
> Affects Versions: 2.14.0, 2.14.1
> Reporter: Paul Rubin
> Assignee: Volkan Yazici
> Priority: Minor
> Fix For: 3.0.0, 2.15.0
>
>
> As reported in LOG4J2-3073, {{TimestampResolver}} of {{JsonTemplateLayout}}
> doesn't format nanoseconds. The suspect is in the following form:
> {code:java}
> formatResolverContext.calendar.setTimeInMillis(timestampMillis);
> formatResolverContext.timestampFormat.format(
> formatResolverContext.calendar,
> formatResolverContext.formattedTimestampBuilder);
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)