A lightweight, modern and flexibly logging utility for R – heavily inspired by the futile.logger
R package and logging
Python module.
The most recent, development version of logger
can also be installed from GitHub:
Setting the log level threshold to something low and logging various messages in ad-hoc and programmatic ways:
library(logger)
log_threshold(DEBUG)
log_info('Script starting up...')
#> INFO [2018-20-11 22:49:36] Script starting up...
pkgs <- available.packages()
log_info('There are {nrow(pkgs)} R packages hosted on CRAN!')
#> INFO [2018-20-11 22:49:37] There are 13433 R packages hosted on CRAN!
for (letter in letters) {
lpkgs <- sum(grepl(letter, pkgs[, 'Package'], ignore.case = TRUE))
log_level(if (lpkgs < 5000) TRACE else DEBUG,
'{lpkgs} R packages including the {shQuote(letter)} letter')
}
#> DEBUG [2018-20-11 22:49:38] 6300 R packages including the 'a' letter
#> DEBUG [2018-20-11 22:49:38] 6772 R packages including the 'e' letter
#> DEBUG [2018-20-11 22:49:38] 5412 R packages including the 'i' letter
#> DEBUG [2018-20-11 22:49:38] 7014 R packages including the 'r' letter
#> DEBUG [2018-20-11 22:49:38] 6402 R packages including the 's' letter
#> DEBUG [2018-20-11 22:49:38] 5864 R packages including the 't' letter
log_warn('There might be many, like {1:2} or more warnings!!!')
#> WARN [2018-20-11 22:49:39] There might be many, like 1 or more warnings!!!
#> WARN [2018-20-11 22:49:39] There might be many, like 2 or more warnings!!!
Setting a custom log layout to render the log records with colors:
library(logger)
log_layout(layout_glue_colors)
log_threshold(TRACE)
log_info('Starting the script...')
log_debug('This is the second log line')
log_trace('Note that the 2nd line is being placed right after the 1st one.')
log_success('Doing pretty well so far!')
log_warn('But beware, as some errors might come :/')
log_error('This is a problem')
log_debug('Note that getting an error is usually bad')
log_error('This is another problem')
log_fatal('The last problem')
Or simply run the related demo:
But you could set up any custom colors and layout, eg using custom colors only for the log levels, make it grayscale, include the calling function or R package namespace with specific colors etc. For more details, see the related vignettes.
Although there are multiple pretty good options already hosted on CRAN when it comes to logging in R, such as
futile.logger
: probably the most popular log4j
variant (and I’m a big fan)logging
: just like Python’s logging
packageloggit
: capture message
, warning
and stop
function messages in a JSON filelog4r
: log4j
-based, object-oriented loggerrsyslog
: logging to syslog
on ‘POSIX’-compatible operating systemslumberjack
: provides a special operator to log changes in dataAlso many more work-in-progress R packages hosted on eg GitHub, such as
But some/most of these packages are
So based on all the above subjective opinions, I decided to write the n+1
th extensible log4j
logger that fits my liking – and hopefully yours as well – with the focus being on:
log4j
glue
when it comes to formatting / rendering log messages, but keep it flexible if others prefer sprintf
(eg for performance reasons) or other functionslog
call can write all the TRACE
log messages to the console, while only pushing ERROR
s to DataDog and eg INFO
messages to CloudWatchWelcome to the Bazaar, and if you have happened to already use any of the above mentioned R packages for logging, you might find useful the Migration Guide.
Check out the main documentation site at https://daroczig.github.io/logger or the vignettes on the below topics: