knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
A lightweight, modern and flexible logging utility for R -- heavily inspired by the futile.logger
R package and logging
Python module.
#| eval: false install.packages("logger")
The most recent, development version of logger
can also be installed from GitHub:
#| eval: false # install.packages("pak") pak::pak("daroczig/logger")
Setting the log level threshold to something low and logging various messages in ad-hoc and programmatic ways:
#| include: false library(logger) log_appender(appender_stdout)
library(logger) log_threshold(DEBUG) log_info("Script starting up...") pkgs <- available.packages() log_info("There are {nrow(pkgs)} 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" ) } log_warn("There might be many, like {1:2} or more warnings!!!")
You can even use a custom log layout to render the log records with colors, as you can see in layout_glue_colors()
:
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 vignette("write_custom_extensions")
.
There are many other logging packages available on CRAN:
futile.logger
: probably the most popular log4j
variant (and I'm a big fan)logging
: just like Python's logging
packagelgr
: built on top of R6.loggit
: 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 dataWhy use logger? I decided to write the n+1
th extensible log4j
logger that fits my liking --- and hopefully yours as well --- with the aim to:
log4j
.glue()
when it comes to formatting / rendering log messages, but keep it flexible if others prefer sprintf()
(e.g. for performance reasons) or other functions.log
call can write all the TRACE
log messages to the console, while only pushing ERROR
s to DataDog and eg INFO
messages to CloudWatch.Welcome to the Bazaar! If you already use any of the above packages for logging, you might find vignette("migration")
useful.
::: .pkgdown-hide
Check out the main documentation site at https://daroczig.github.io/logger/ or the vignettes on the below topics:
:::
If you prefer visual content, you can watch the video recording of the "Getting things logged" talk at RStudio::conf(2020):
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.