1919import java .io .PrintWriter ;
2020import java .text .DateFormat ;
2121import java .text .SimpleDateFormat ;
22+ import java .time .Instant ;
2223
2324/**
2425 * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
@@ -34,12 +35,31 @@ public class ThrowableCallStack implements CallStack {
3435 * A snapshot of a throwable.
3536 */
3637 private static final class Snapshot extends Throwable {
38+
3739 private static final long serialVersionUID = 1L ;
38- private final long timestampMillis = System .currentTimeMillis ();
40+ private final Instant timestamp ;
41+
42+ /**
43+ * Constructs a new instance with its message set to the now instant.
44+ */
45+ public Snapshot () {
46+ this (Instant .now ());
47+ }
48+
49+ /**
50+ * Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
51+ *
52+ * @param timestamp normally the now instant.
53+ */
54+ private Snapshot (final Instant timestamp ) {
55+ super (timestamp .toString ());
56+ this .timestamp = timestamp ;
57+ }
3958 }
4059
4160 private final String messageFormat ;
4261
62+ // We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
4363 //@GuardedBy("dateFormat")
4464 private final DateFormat dateFormat ;
4565
@@ -77,7 +97,8 @@ public synchronized boolean printStackTrace(final PrintWriter writer) {
7797 message = messageFormat ;
7898 } else {
7999 synchronized (dateFormat ) {
80- message = dateFormat .format (Long .valueOf (snapshotRef .timestampMillis ));
100+ // The throwable message is in {@link DateTimeFormatter#ISO_INSTANT} format for more precision.
101+ message = dateFormat .format (Long .valueOf (snapshotRef .timestamp .toEpochMilli ()));
81102 }
82103 }
83104 writer .println (message );
0 commit comments