lgr-simple

library(lgr)

Simplified Logging Syntax

If you care about terse syntax you can use the following UPPERCASE1 commands to log:

FATAL("This is an important message about %s going wrong", "something")
#> FATAL [12:46:18.137] This is an important message about something going wrong
ERROR("A less severe error")
#> ERROR [12:46:18.165] A less severe error
WARN("something likely went wrong")
#> WARN  [12:46:18.207] something likely went wrong
INFO("Everything is ok")
#> INFO  [12:46:18.209] Everything is ok
DEBUG("Debug messages are hidden by default")
console_threshold("debug")  # you must use lower case names here
DEBUG("unless we lower the threshold")
#> DEBUG [12:46:18.213] unless we lower the threshold
TRACE("Trace is even lower than debug")

If you have the package data.table installed, lgr does not only log to the console, but also to an in memory data.table. You can use show_log() to quickly display this in memory log2.

show_log(3)  # show the last 3 log entries

If you looked carefully, you see that both debug messages were logged to memory (not only the one that you saw in the console). This is explained in the section on thresholds.


  1. If you are wondering why I decided on ALL-CAPS for the logging functions: It distinguishes log calls well from normal code, and also all the good names are already taken by base R functions (log(), trace(), debug()). Many R packages deal with this issue by using prefixes (like log_debug()), but I do not like that idea. In any case that is pretty to close to what you get when you use the R6 logging syntax

  2. If you want to know more about interacting with the data.table Appender see the help for ?AppenderDt.