Skip to main content

Pattern rendering

You can configure rendering using a pattern, like pattern layouts for Log4j and Logback. Pattern rendering is best suited to console output.

You can specify a pattern using the configuration DSL, for example:

  sink(
"console",
RenderPattern("%t{LOCAL_TIME} %-5v{COLOUR} [%-10c] - %30l - %m - %i%s"),
STDOUT
)
// etc.

Here is a JSON configuration example:

{
"sinks": {
"console": {
"renderPattern": "%t{LOCAL_TIME} %-5v{COLOUR} [%-10c] - %30l - %m - %i%s",
"sendTo": "STDOUT"
}
}
// etc.
}

Pattern language

Patterns contain:

  • Fixed text that is included in every output.
  • Tokens representing log event fields with optional size specification. Some tokens can be followed by a format specifier. Examples are %t, %-10l and %-5v{COLOUR}.

Tokens

The pattern language includes the following fields, each specified using % and a single letter:

TokenFieldNotes
%tTimestampDefault format is ISO8601, e.g. 2024-11-25T05:52:15.286632Z.
%hHost
%lLogger name
%cContext
%vLogging level
%mLog messageIf the message is a template, it is evaluated before being output.
%sStack traceOnly output if present, preceded by a newline.
%iItems
%nNewlineOutput a single newline character.

Size specification

A size specification is put between % and the token letter. It is used to fit the field value into a fixed width, either left-or right aligned.

Zero

  • Show the field value full width, the same as omitting the size specification.

Positive

  • If the field value is longer than the size, maybe shorten it (see below) and show the left-most characters.
  • If the value is shorter, left-align it in the width specified by the size.
  • For stack traces, sets the maximum number of lines to show.

Negative

  • If the field value is longer than the size, maybe shorten it (see below) and show the left-most characters.
  • If the value is shorter, right-align it in the width specified by the size.

Notes:

  • A size specification for the %n newline token is ignored.
  • Host, logger name and context values are shortened if they are longer than the specified size.

Some example output from the pattern "%t{LOCAL_TIME} %-5v [%-20c] - %20l - %m - %i%2s" is:

16:55:45.014298  INFO [  D-worker-1+Playpen] - i.k.e.KloggerPlaypen - Event 3 at 2024-11-25T16:55:45.014279 - {run=ad085c84-35b2-4700-a57d-14a382d009a9, Counter=3, Iteration=3}
16:55:45.040453 ERROR [ D-worker-3+Playpen] - i.k.e.KloggerPlaypen - Message: Oops! Something went wrong - {run=ad085c84-35b2-4700-a57d-14a382d009a9, Message=Oops! Something went wrong}
java.lang.RuntimeException: Oops! Something went wrong
at io.klogging.PlaypenKt.functionWithException(Playpen.kt:159)

Format specification

The %t and %v tokens can take a format specifier.

FormatEffect
%t{LOCAL_TIME}Output only the time component of the event timestamp in the local timezone, instead of ISO8601 timestamp.
%v{COLOUR} or
%v{COLOR}
Output log levels with a different colour for each level, as for RENDER_ANSI.

Level colours are (approximately):

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

You can combine size and format specifications.