lgr (version 0.3.3)

LogEvent: Events - The Atomic Unit of Logging

Description

A LogEvent is a single unit of data that should be logged. LogEvents are usually created by a Logger, and then processed by Appenders.

Arguments

Usage

x <- LogEvent$new(logger, level = 400, timestamp = Sys.time(), caller = NA, msg
  = NA, ...)

x$clone(deep = FALSE)

x$.logger x$caller x$level x$level_name x$logger x$msg x$timestamp x$values

Creating LogEvents / Fields

The arguments to LogEvent$new() directly translate to the fields stored in the LogEvent:

level

integer: the log_level / priority of the LogEvent

timestamp

POSIXct the time when then the LogEvent was created

caller

character. The name of the calling function

msg

character. A message

logger

character scalar. Name of the Logger that created the event (.logger$full_name)

user

character scalar. User as set for the Logger that created this event (.logger$user)

.logger

a Logger. A reference to the Logger that created the event

...

All named arguments in ... will be added to the LogEvent as custom fields. You can store arbitrary R objects in LogEvents this way, but not all Appenders will support them. See AppenderJson for an Appender that supports custom fields quite naturally.

Usually the above values will be scalars, but (except for "logger") they can also be vectors if they are all of the same length (or scalars that will be recycled). In this case the event will be treated by the Appenders and Layouts as if several separate events.

Active Bindings

LogEvents contain some some active bindings that make it easier to retrieve commonly used values.

level_name

character: the log_level / priority of the LogEvent labelled according to getOption("lgr.log_levels")

values

list: All values stored in the LogEvent (including all custom fields, but not including event$logger)

logger_name

character scalar: The name of the Logger that created this event, equivalent to event$logger$name)

logger_user

character scalar: The user of the Logger that created this event, equivalent to event$logger_user)

See Also

as.data.frame.LogEvent()

Examples

Run this code
# NOT RUN {
lg <- get_logger("test")
lg$error("foo bar")

# The last LogEvent produced by a Logger is stored in the last_event field
lg$last_event  # formatted by default
lg$last_event$values  # values stored in the event

# Also contains the Logger that created it as .logger
lg$last_event$logger
# equivalent to
lg$last_event$.logger$name

# This is really a reference to the complete Logger, so the following is
# possible (though nonsensical)
lg$last_event$.logger$last_event$msg
identical(lg, lg$last_event$.logger)
lg$config(NULL)  # reset logger config
# }

Run the code above in your browser using DataLab