KeyValueFormatter

public final class KeyValueFormatter implements MetadataKey.KeyValueHandler

Formats key/value pairs as a human readable string on the end of log statements. The format is:

  Log Message PREFIX[ key1=value1 key2=value2 ]
or
  Multi line
  Log Message
  PREFIX[ key1=value1 key2=value2 ]
Note that:
  • Key/value pairs are appended in the order they are handled.
  • If no key/value pairs are handled, the log message is unchanged (no prefix is added).
  • Keys can be repeated.
  • Key labels do not need quoting.
  • String-like values are properly quoted and escaped (e.g. \", \\, \n, \t)
  • Unsafe control characters in string-like values are replaced by U+FFFD (�).
  • All key/value pairs are on the "same line" of the log message.
The result is that this string should be fully reparsable (with the exception of replaced unsafe characters) and easily searchable by text based tools such as "grep".

Constructors

Link copied to clipboard
public void KeyValueFormatter(String prefix, String suffix, StringBuilder out)
Creates a formatter using the given prefix to append key/value pairs to the current log message.

Functions

Link copied to clipboard
public static void appendJsonFormattedKeyAndValue(String label, Object value, StringBuilder out)
Helper method to emit metadata key/value pairs in a format consistent with JSON.
Link copied to clipboard
public void done()
Terminates handling of key/value pairs, leaving the originally supplied buffer modified.
Link copied to clipboard
public void handle(String label, @Nullable() Object value)
Handle a single key/value a pair of contextual metadata for a log statement.