Learn R Programming

lgr (version 0.3.4)

LayoutFormat: Format Log Events as Text

Description

Format a LogEvent as human readable text using format.LogEvent(), which provides a quick and easy way to customize log messages. If you need more control and flexibility, consider using LayoutGlue instead.

Arguments

Usage

x <- LayoutFormat$new(fmt = "%L [%t] %m", timestamp_fmt = "%Y-%m-%d
  %H:%M:%OS3", colors = NULL, pad_levels = "right")

x$clone(deep = FALSE) x$format_event(event) x$set_colors(x) x$set_fmt(x) x$set_pad_levels(x) x$set_timestamp_fmt(x) x$toString()

x$colors x$fmt x$pad_levels x$timestamp_fmt

Creating a New LayoutFormat

A new LayoutFormat is instantiated with LayoutFormat$new(). For a description of the arguments to this function please refer to the Fields, and the documentation of format.LogEvent().

Fields

fmt

a character scalar containing format tokens. See format.LogEvent().

timestamp_fmt

a character scalar. See base::format.POSIXct()

colors

a named list of functions passed on on format.LogEvent()

pad_levels

right, left or NULL. See format.LogEvent()

Format Tokens

This is the same list of format tokens as for format.LogEvent()

%t

The timestamp of the message, formatted according to timestamp_fmt)

%l

the log level, lowercase character representation

%L

the log level, uppercase character representation

%k

the log level, first letter of lowercase character representation

%K

the log level, first letter of uppercase character representation

%n

the log level, integer representation

%p

the PID (process ID). Useful when logging code that uses multiple threads.

%c

the calling function

%m

the log message

%f

all custom fields of x in a pseudo-JSON like format that is optimized for human readability and console output

%j

all custom fields of x in proper JSON. This requires that you have jsonlite installed and does not support colors as opposed to %f

Methods

format_event(event)

format a LogEvent

See Also

Other Layouts: LayoutDbi, LayoutGlue, LayoutJson, Layout

Examples

Run this code
# NOT RUN {
# setup a dummy LogEvent
event <- LogEvent$new(
  logger = Logger$new("dummy logger"),
  level = 200,
  timestamp = Sys.time(),
  caller = NA_character_,
  msg = "a test message"
)
lo <- LayoutFormat$new()
lo$format_event(event)

# }

Run the code above in your browser using DataLab