Interface LogFormatter.TimestampFormatter

All Superinterfaces:
LogFormatter
All Known Implementing Classes:
LogFormatter.NoopFormatter
Enclosing interface:
LogFormatter

public static sealed interface LogFormatter.TimestampFormatter extends LogFormatter permits LogFormatter.NoopFormatter (not exhaustive)
Formats event timestamps.
  • Field Details

    • TTLL_TIME_FORMAT

      static final String TTLL_TIME_FORMAT
      The default timestamp format used in many logging frameworks which does not have dates and only time at millisecond precision.

      It is called TTLL as that is the name of the format where it is used in logback, log4j etc.

      See Also:
  • Method Details

    • formatTimestamp

      void formatTimestamp(StringBuilder output, Instant instant)
      Format timestamp.
      Parameters:
      output - buffer.
      instant - timestamp.
    • format

      default void format(StringBuilder output, LogEvent event)
      Description copied from interface: LogFormatter
      Formats a log event.
      Specified by:
      format in interface LogFormatter
      Parameters:
      output - buffer.
      event - log event.
      See Also:
    • of

      Formats timestamp using TTLL_TIME_FORMAT.
      Returns:
      formatter.
    • ofISO

      Formats a timestamp using ISO format at fixed millisecond precision (e.g. 2023-11-14T22:13:20.123Z, always exactly 3 fractional digits). Pair with ofMicros() to append additional sub-millisecond digits, similar to Logback's %d{ISO8601}%microsecond.
      Returns:
      formatter.
    • ofMicros

      Micro seconds over the events last second. This is for logback compatibility.
      Returns:
      microseconds zero padded.
    • of

      Formats a timestamp using standard JDK date time formatter.
      Parameters:
      dateTimeFormatter - date time formatter.
      Returns:
      timestamp formatter.
    • of

      static LogFormatter.TimestampFormatter of(DateTimeFormatter dateTimeFormatter, boolean cacheMillisecondPrecision)
      Formats a timestamp using standard JDK date time formatter.

      Formatting with DateTimeFormatter is comparatively expensive to do on every event, so if the caller knows the formatter's finest resolution is milliseconds (e.g. a pattern using at most SSS, never n/N nanosecond fields), cacheMillisecondPrecision can be set to true to reuse the previously formatted string whenever consecutive events land in the same millisecond - the same trick Logback's CachingDateFormatter and Log4j2's FixedDateFormat use. Passing true for a formatter that can actually render sub-millisecond precision will silently truncate that precision, so only pass true when the pattern is known to not exceed millisecond resolution.

      Parameters:
      dateTimeFormatter - date time formatter.
      cacheMillisecondPrecision - true if the formatter's output is fully determined by the instant's millisecond value.
      Returns:
      timestamp formatter.