Skip to content

Commit

Permalink
Logback appender: map timestamp in nanoseconds if possible (#11807)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoslet committed Jul 12, 2024
1 parent 54622be commit e0cfdc4
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.opentelemetry.semconv.ExceptionAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -106,8 +107,21 @@ private void mapLoggingEvent(
}

// time
long timestamp = loggingEvent.getTimeStamp();
builder.setTimestamp(timestamp, TimeUnit.MILLISECONDS);
Instant instant = loggingEvent.getInstant(); // instant can be null
if (instant != null) {
builder.setTimestamp(instant);
}
else {
long timestamp = loggingEvent.getTimeStamp();
int nanoseconds = loggingEvent.getNanoseconds(); // nanoseconds can be -1
if (nanoseconds != -1) {
timestamp = TimeUnit.MILLISECONDS.toNanos(timestamp) + nano;
builder.setTimestamp(timestamp, TimeUnit.NANOSECONDS);
}
else {
builder.setTimestamp(timestamp, TimeUnit.MILLISECONDS);
}
}

// level
Level level = loggingEvent.getLevel();
Expand Down

0 comments on commit e0cfdc4

Please sign in to comment.