lgr (version 0.3.3)

AppenderConsole: Log to the console

Description

A simple Appender that outputs to the console. If you have the package crayon installed log levels will be coloured by default (but you can modify this behaviour by passing a custom Layout).

Arguments

Usage

x <- AppenderConsole$new(threshold = NA_integer_, layout = LayoutFormat$new(fmt
  = "%L [%t] %m %f", timestamp_fmt = "%H:%M:%OS3", colors =
  getOption("lgr.colors", list())), filters = NULL)

x$add_filter(filter, name = NULL) x$append(event) x$filter(event) x$format(color = FALSE, ...) x$remove_filter(pos) x$set_filters(filters) x$set_layout(layout) x$set_threshold(level)

x$destination x$filters x$layout x$threshold

Creating a New Appender

New Appenders are instantiated with <AppenderSubclass>$new(). For the arguments to new() please refer to the section Fields. You can also modify those fields after the Appender has been created with setters in the form of appender$set_<fieldname>(value)

Fields

threshold, set_threshold(level)

character or integer scalar. The minimum log level that triggers this logger. See log_levels

layout, set_layout(layout)

a Layout that will be used for formatting the LogEvents passed to this Appender

destination

The output destination of the Appender in human-readable form (mainly for print output)

filters, set_filters(filters)

a list that may contain functions or any R object with a filter() method. These functions must have exactly one argument: event which will get passed the LogEvent when the Filterable's filter() method is invoked. If all of these functions evaluate to TRUE the LogEvent is passed on. Since LogEvents have reference semantics, filters can also be abused to modify them before they are passed on. Look at the source code of with_log_level() or with_log_value() for examples.

Methods

append(event)

Tell the Appender to process a LogEvent event. This method is usually not called by the user, but invoked by a Logger

filter(event)

Determine whether the LogEvent x should be passed on to Appenders (TRUE) or not (FALSE). See also the active binding filters

add_filter(filter, name = NULL), remove_filter(pos)

Add or remove a filter. When adding a filter an optional name can be specified. remove_filter() can remove by position or name (if one was specified)

See Also

LayoutFormat

Other Appenders: AppenderBuffer, AppenderDbi, AppenderFileRotating, AppenderFile, AppenderGmail, AppenderJson, AppenderPushbullet, AppenderRjdbc, AppenderSendmail, AppenderSyslog, AppenderTable, Appender

Examples

Run this code
# NOT RUN {
# create a new logger with propagate = FALSE to prevent routing to the root
# logger. Please look at the section "Logger Hirarchies" in the package
# vignette for more info.
lg  <- get_logger("test")$set_propagate(FALSE)

lg$add_appender(AppenderConsole$new())
lg$add_appender(AppenderConsole$new(
  layout = LayoutFormat$new("[%t] %c(): [%n] %m", colors = getOption("lgr.colors"))))

# Will output the message twice because we attached two console appenders
lg$warn("A test message")
lg$config(NULL) # reset config
# }

Run the code above in your browser using DataLab