layouts: Format logs with Layouts

layoutsR Documentation

Format logs with Layouts

Description

In log4j etymology, Layouts are how Appenders control the format of messages. Most users will use one of the general-purpose layouts provided by the package:

  • default_log_layout() formats messages much like the original log4j library. simple_log_layout() does the same, but omits the timestamp.

  • bare_log_layout() emits only the log message, with no level or timestamp fields.

  • logfmt_log_layout() and json_log_layout() format structured logs in the two most popular machine-readable formats.

For implementing your own layouts, see Details.

Usage

default_log_layout(time_format = "%Y-%m-%d %H:%M:%S")

simple_log_layout()

bare_log_layout()

logfmt_log_layout()

json_log_layout()

Arguments

time_format

A valid format string for timestamps. See base::strptime().

Details

Layouts return a function with the signature ⁠function(level, ...)⁠ that itself returns a single newline-terminated string. Anything that meets this interface can be passed as a layout to one of the existing appenders.

json_log_layout requires the jsonlite package.

Examples

# The behaviour of a layout can be seen by using them directly:
simple <- simple_log_layout()
simple("INFO", "Input has length ", 0, ".")

with_timestamp <- default_log_layout()
with_timestamp("INFO", "Input has length ", 0, ".")

logfmt <- logfmt_log_layout()
logfmt("INFO", msg = "got input", length = 24)

johnmyleswhite/log4r documentation built on Oct. 22, 2024, 8 p.m.