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. If you have
configured the root logger with
basic_config(memory = TRUE)
, it will have an
AppenderBuffer that logs all log messages (including TRACE
and DEBUG
),
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.
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)
passed on to base::sprintf()
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
an integer
or character
scalar, see
getOption("lgr.log_levels")
for possible values. For threshold
0
("off"
) and NA
("all"
) are also valid.
an Appender
character
scalar. An optional name for the new Appender.
integer
index or character
names of the appenders to remove
integer
scalar. Show only the last n
log entries that match
threshold
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
.
# NOT RUN {
add_appender(AppenderConsole$new(), "second_console_appender")
lgr$fatal("Multiple console appenders are a bad idea")
remove_appender("second_console_appender")
lgr$info("Good that we defined an appender name, so it's easy to remove")
# }
Run the code above in your browser using DataLab