lgr provides convenience functions managing the root Logger. These are designed chiefly for interactive use and are less verbose than their R6 method counterparts.

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. (equivalent to lgr::lgr$threshold and lgr::lgr$set_threshold)

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. (equivalent to lgr::lgr$appenders$console$threshold and lgr::lgr$appenders$console$set_threshold)

add_appender() and remove_appender() add Appenders to Loggers and other Appenders. (equivalent to lgr::lgr$add_appender and lgr::lgr$remove_appender)

show_log() displays the last n log entries of an Appender (or a Logger with such an Appender attached) with a $show() method. Most, but not all Appenders support this function (try AppenderFile or AppenderBuffer).

show_data() and show_dt() work similar to show_log(), except that they return the log as data.frame or data.table respectively. Only Appenders that log to formats that can easily be converted to data.frames are supported (try AppenderJson or AppenderBuffer).

The easiest way to try out this features is by adding an AppenderBuffer to the root logger with basic_config(memory = TRUE).

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)



Any R code


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


a character scalar. The name of the calling function


integer or character scalar: the desired log level


a Logger or Appender or the name of a Logger as character scalar


an Appender


character scalar. An optional name for the new Appender.


integer index or character names of the appenders to remove


character or integer scalar. The minimum log level that should be processed by the root logger.


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


threshold() and console_threshold() return the log_level of targetas 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 character vector, data.frame or data.table (invisibly). show_data() always returns a data.frame and show_dt() always returns a data.table.


# Get and set the threshold of the root logger
#> [1] 200
lgr$info("this will be supressed")
lgr$error("an important error message")
#> ERROR [06:20:19.673] an important error message

# you can also specify a target to modify other loggers
lg <- get_logger("test")
threshold("fatal", target = lg)
threshold(target = lg)
#> [1] 100

# If a Logger's threshold is not set, the threshold is inherited from
# its parent, in this case the root logger (that we set to error/200 before)
threshold(NULL, target = lg)
threshold(target = lg)
#> [1] 200

# Alternative R6 API for getting/setting thresholds
#> [1] 400
#> [1] 300
#> [1] 200

# cleanup
#> <LoggerRoot> [info] root
#> <Logger> [info] test

# add Appenders to a Logger
add_appender(AppenderConsole$new(), "second_console_appender")
lgr$fatal("Multiple console appenders are a bad idea")
#> FATAL [06:20:19.694] Multiple console appenders are a bad idea
lgr$info("Good that we defined an appender name, so it's easy to remove")

# Reconfigure the root logger
basic_config(memory = TRUE)
#> <LoggerRoot> [info] root
#> appenders:
#>   console: <AppenderConsole> [all] -> console
#>   memory : <AppenderBuffer>  [all] -> 0 child Appenders

# log some messages
lgr$info("a log message")
#> INFO  [06:20:19.704] a log message
lgr$info("another message with data", data = 1:3)
#> INFO  [06:20:19.707] another message with data {data: (1, 2, 3)}

#> INFO  [06:20:19] a log message
#> INFO  [06:20:19] another message with data
#>   level           timestamp logger caller                       msg    data
#> 1   400 2022-05-31 06:20:19   root   eval             a log message    NULL
#> 2   400 2022-05-31 06:20:19   root   eval another message with data 1, 2, 3

# cleanup
#> <LoggerRoot> [info] root