Logback appender: map timestamp in nanoseconds if possible (#11807) (#11974)

This commit is contained in:
Jérôme Joslet 2024-08-15 00:52:53 +02:00 committed by GitHub
parent 31e5eea3bc
commit dee515d197
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 2 deletions

View File

@ -45,6 +45,7 @@ public final class LoggingEventMapper {
private static final AttributeKey<Long> THREAD_ID = AttributeKey.longKey("thread.id");
private static final AttributeKey<String> THREAD_NAME = AttributeKey.stringKey("thread.name");
private static final boolean supportsInstant = supportsInstant();
private static final boolean supportsKeyValuePairs = supportsKeyValuePairs();
private static final boolean supportsMultipleMarkers = supportsMultipleMarkers();
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
@ -106,8 +107,12 @@ public final class LoggingEventMapper {
}
// time
if (supportsInstant && hasInstant(loggingEvent)) {
setTimestampFromInstant(builder, loggingEvent);
} else {
long timestamp = loggingEvent.getTimeStamp();
builder.setTimestamp(timestamp, TimeUnit.MILLISECONDS);
}
// level
Level level = loggingEvent.getLevel();
@ -174,6 +179,28 @@ public final class LoggingEventMapper {
builder.setContext(Context.current());
}
// getInstant is available since Logback 1.3
private static boolean supportsInstant() {
try {
ILoggingEvent.class.getMethod("getInstant");
} catch (NoSuchMethodException e) {
return false;
}
return true;
}
@NoMuzzle
private static boolean hasInstant(ILoggingEvent loggingEvent) {
return loggingEvent.getInstant() != null;
}
@NoMuzzle
private static void setTimestampFromInstant(
LogRecordBuilder builder, ILoggingEvent loggingEvent) {
builder.setTimestamp(loggingEvent.getInstant());
}
// visible for testing
void captureMdcAttributes(AttributesBuilder attributes, Map<String, String> mdcProperties) {
if (captureAllMdcAttributes) {