Format Log Events as Text

Format Log Events as Text

Details

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.

see Fields

see Fields

see Fields

see Fields

Convert Layout to a character string Read a log file written using LayoutFormat

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

%g

the name of the logger

%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

See also

Other Layouts: LayoutGlue, LayoutJson, Layout

Super class

lgr::Layout -> LayoutFormat

Active bindings

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 (like the ones provided by the package crayon) passed on on format.LogEvent().

pad_levels

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

Methods

Inherited methods


    Method new()

    Usage

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


    Method format_event()

    Format a LogEvent

    Usage

    LayoutFormat$format_event(event)

    Arguments

    event

    a LogEvent


    Method set_fmt()

    Usage

    LayoutFormat$set_fmt(x)


    Method set_timestamp_fmt()

    Usage

    LayoutFormat$set_timestamp_fmt(x)


    Method set_colors()

    Usage

    LayoutFormat$set_colors(x)


    Method set_pad_levels()

    Usage

    LayoutFormat$set_pad_levels(x)


    Method toString()

    Usage

    LayoutFormat$toString()


    Method read()

    Usage

    LayoutFormat$read(file, threshold = NA_integer_, n = 20L)

    Arguments

    threshold

    a character or integer threshold

    n

    number of log entries to display


    Method clone()

    The objects of this class are cloneable with this method.

    Usage

    LayoutFormat$clone(deep = FALSE)

    Arguments

    deep

    Whether to make a deep clone.

    Examples

    # 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)
    #> [1] "ERROR [2023-03-04 20:04:29.157] a test message"