Learn R Programming

lgr (version 0.2.0)

simple_logging: Simple Logging

Description

These functions provide a simple interface to the root logger. If you do not need any of the more advanced features of lgr, start here.

lgr provides convenience functions to manage the root Logger. These are intended for interactive use, and for people who just need basic logging facilities and don't want to worry about hierarchical loggers and R6 classes.

threshold() sets or retrieves the threshold for an Appender or Logger (the minimum level of log messages it processes). It's target defaults to the root logger.

console_threshold() is a shortcut to set the threshold of the root loggers AppenderConsole, which is usually the only Appender that manages console output for a given R session.

add_appender() and remove_appender() add Appenders to Loggers and other Appenders.

show_log() displays the last n log entries of target if target is an Appender with a show() method or a Logger with at least one such Appender attached. target defaults to the root logger, which has an AppenderDt attached by default if the package data.table is installed. With the default logging settings this includes also TRACE and DEBUG messages, even if they were not printed to the console before.

show_data() and show_dt() work similar to show_log(), except that they return the log as data.frame or data.table respectively.

Usage

FATAL(msg, ...)

ERROR(msg, ...)

WARN(msg, ...)

INFO(msg, ...)

DEBUG(msg, ...)

TRACE(msg, ...)

log_exception(code, logfun = lgr$fatal, caller = get_caller(-3))

threshold(level, target = lgr::lgr)

console_threshold(level, target = lgr::lgr$appenders$console)

add_appender(appender, name = NULL, target = lgr::lgr)

remove_appender(pos, target = lgr::lgr)

show_log(threshold = NA_integer_, n = 20L, target = lgr::lgr)

show_dt(target = lgr::lgr)

show_data(target = lgr::lgr)

Arguments

msg, ...

passed on to base::sprintf()

code

Any R code

logfun

a function for processing the log request, usually lgr$info(), lgr$debug(), etc... .

caller

a character scalar. The name of the calling function

level, threshold

an integer or character scalar, see getOption("lgr.log_levels") for possible values. For threshold 0 ("off") and NA ("all") are also valid.

target

a Logger or Appender. Defaults to the root logger.

appender

an Appender

name

character scalar. An optional name for the new Appender.

pos

integer index or character names of the appenders to remove

n

integer scalar. Show only the last n log entries that match threshold

Value

FATAL() ... TRACE() and log_exception() return the log message as a character vector.

threshold() and console_threshold() return the log_level of target as integer (invisibly)

add_appender() and remove_appender() return target.

show_log() prints to the console and returns whatever the target Appender's $show() method returns, usually a data.frame or data.table (invisibly).

show_data() always returns a data.frame and show_dt() always returns a data.table.

Examples

Run this code
# NOT RUN {
FATAL("This is an important message about %s going wrong", "something")
DEBUG("Debug messages are hidden by default")
console_threshold("debug")  # you must use lower case names here
DEBUG("Unless we lower the threshold")

add_appender(AppenderConsole$new(), "second_console_appender")
FATAL("Multiple console appenders are a bad idea")
remove_appender("second_console_appender")
INFO("Good that we defined an appender name, so it's easy to remove")
# }

Run the code above in your browser using DataLab