AppenderDt: Log to an in-memory data.table

AppenderDtR Documentation

Log to an in-memory data.table

Description

An Appender that outputs to an in-memory data.table. It fulfill a similar purpose as the more flexible AppenderBuffer and is mainly included for historical reasons/backwards compatibility with older version of lgr.

NOTE: AppenderDt has been superseded by lgr::AppenderBuffer and is kept mainly for archival purposes.

Value

The $new() method returns an R6::R6 that inherits from lgr::Appender and can be uses as an appender by a lgr::Logger.

Custom Fields

AppenderDt supports custom fields, but they have to be pre-allocated in the prototype argument. Custom fields that are not part of the prototype are inserted in the list-column .fields if it exists.

Creating a Data Table Appender

In addition to the usual fields, AppenderDt$new() requires that you supply a buffer_size and a prototype. These determine the structure of the data.table used to store the log this appender creates and cannot be modified anymore after the instantiation of the appender.

The lgr::Layout for this Appender is used only to format console output of its $show() method.

Comparison AppenderBuffer and AppenderDt

Both AppenderBuffer and AppenderDt do in memory buffering of events. AppenderBuffer retains a copies of the events it processes and has the ability to pass the buffered events on to other Appenders. AppenderDt converts the events to rows in a data.table and is a bit harder to configure. Used inside loops (several hundred iterations), AppenderDt has much less overhead than AppenderBuffer. For single logging calls and small loops, AppenderBuffer is more performant. This is related to how memory pre-allocation is handled by the appenders.

Super classes

lgr::Filterable -> lgr::Appender -> AppenderDt

Methods

Public methods

Inherited methods

Method new()

Creating a new AppenderDt

Usage
AppenderDt$new(
  threshold = NA_integer_,
  layout = LayoutFormat$new(fmt = "%L [%t] %m %f", timestamp_fmt = "%H:%M:%OS3",
    colors = getOption("lgr.colors", list())),
  prototype = data.table::data.table(.id = NA_integer_, level = NA_integer_, timestamp =
    Sys.time(), logger = NA_character_, caller = NA_character_, msg = NA_character_,
    .fields = list(list())),
  buffer_size = 1e+05,
  filters = NULL
)
Arguments
prototype

A prototype data.table. The prototype must be a data.table with the same columns and column types as the data you want to log. The actual content of the columns is irrelevant. There are a few reserved column names that have special meaning: * .id: integer (mandatory). Must always be the first column and is used internally by the Appender * .fields: list (optional). If present all custom values of the event (that are not already part of the prototype) are stored in this list column.

buffer_size

integer scalar. Number of rows of the in-memory data.table


Method append()

Usage
AppenderDt$append(event)

Method show()

Usage
AppenderDt$show(threshold = NA_integer_, n = 20L)

Method set_layout()

Usage
AppenderDt$set_layout(layout)

See Also

data.table::data.table

Other Appenders: AppenderDbi, AppenderElasticSearch, AppenderGmail, AppenderPushbullet, AppenderSendmail, AppenderSyslog

Examples

lg <- lgr::get_logger("test")
lg$config(list(
  appenders = list(memory = AppenderDt$new()),
  threshold = NA,
  propagate = FALSE  # to prevent routing to root logger for this example
))
lg$debug("test")
lg$error("test")

# Displaying the log
lg$appenders$memory$data
lg$appenders$memory$show()
lgr::show_log(target = lg$appenders$memory)

# If you pass a Logger to show_log(), it looks for the first AppenderDt
# that it can find.
lgr::show_log(target = lg)

# Custom fields are stored in the list column .fields by default
lg$info("the iris data frame", caps = LETTERS[1:5])
lg$appenders$memory$data
lg$appenders$memory$data$.fields[[3]]$caps
lg$config(NULL)

lgrExtra documentation built on Jan. 6, 2023, 5:15 p.m.