The following vignette presents benchmarks for log4r against all general-purpose logging packages available on CRAN:
Each logging package features slightly different capabilities, but these benchmarks are focused on the two situations common to using all of them:
The first of these is likely the most common kind of logging done by end users, although some may chose to log to files, over HTTP, or to the system log (among others). Yet a benchmark of these other scenarios would largely show the relative expense of these operations, instead of the overhead of the logic performed by the logging packages themselves.
The second measures the performance impact of leaving logging messages in running code, even if they are below the current threshold of visibility. This is another measure of overhead for each logging package.
cat()
As a reference point, we can measure how long it takes R itself to write a simple message to the console:
<- function() {
cat_debug cat() # Print nothing.
}
<- function() cat(
cat_info "INFO [", format(Sys.time(), "%Y-%m-%d %H:%M:%S", usetz = FALSE),
"] Info message.", sep = ""
)
The following is a typical log4r setup:
<- log4r::logger(threshold = "INFO")
log4r_logger
<- function() {
log4r_info ::info(log4r_logger, "Info message.")
log4r
}
<- function() {
log4r_debug ::debug(log4r_logger, "Debug message.")
log4r }
The following is a typical futile.logger setup:
requireNamespace("futile.logger")
#> Loading required namespace: futile.logger
::flog.logger()
futile.logger
<- function() {
fl_info ::flog.info("Info message.")
futile.logger
}
<- function() {
fl_debug ::flog.debug("Debug message.")
futile.logger }
The following is what I believe to be a typical logging setup:
requireNamespace("logging")
#> Loading required namespace: logging
::basicConfig()
logging
<- function() {
logging_info ::loginfo("Info message.")
logging
}
<- function() {
logging_debug ::logdebug("Debug message.")
logging }
The following is what I believe to be a typical logger setup:
requireNamespace("logger")
#> Loading required namespace: logger
# Match the behaviour of other logging packages and write to the console.
::log_appender(logger::appender_stdout)
logger
<- function() {
logger_info ::log_info("Info message.")
logger
}
<- function() {
logger_debug ::log_debug("Debug message.")
logger }
The following is what I believe to be a typical lgr setup:
requireNamespace("lgr")
#> Loading required namespace: lgr
<- lgr::get_logger("perf-test")
lgr_logger $set_appenders(list(cons = lgr::AppenderConsole$new()))
lgr_logger$set_propagate(FALSE)
lgr_logger
<- function() {
lgr_info $info("Info message.")
lgr_logger
}
<- function() {
lgr_debug $debug("Debug message.")
lgr_logger }
The following is what I believe to be a typical loggit setup. Since we only want to log to the console, set the output file to /dev/null
. In addition, loggit does not have a notion of thresholds, so there is no “do nothing” operation to test.
requireNamespace("loggit")
#> Loading required namespace: loggit
if (.Platform$OS.type == "unix") {
::set_logfile("/dev/null")
loggitelse {
} ::set_logfile("nul")
loggit
}
<- function() {
loggit_info ::loggit("INFO", "Info message.")
loggit }
The rlog package currently has no configuration options other than the threshold, which is controlled via an environment variable and defaults to hiding debug-level messages:
requireNamespace("rlog")
#> Loading required namespace: rlog
<- function() {
rlog_info ::log_info("Info message.")
rlog
}
<- function() {
rlog_debug ::log_debug("Debug message.")
rlog }
Debug messages should print nothing.
log4r_debug()
cat_debug()
logging_debug()
fl_debug()
logger_debug()
lgr_debug()
rlog_debug()
Info messages should print to the console. Small differences in output format are to be expected.
log4r_info()
#> INFO [2021-10-25 18:15:10] Info message.
cat_info()
#> INFO [2021-10-25 18:15:10] Info message.
logging_info()
#> 2021-10-25 18:15:10 INFO::Info message.
fl_info()
#> INFO [2021-10-25 18:15:10] Info message.
logger_info()
#> INFO [2021-10-25 18:15:10] Info message.
lgr_info()
#> INFO [18:15:10.053] Info message.
loggit_info()
#> {"timestamp": "2021-10-25T18:15:10+0000", "log_lvl": "INFO", "log_msg": "Info message."}
rlog_info()
#> 2021-10-25 18:15:10 [INFO] Info message.
The following benchmarks all loggers defined above:
<- microbenchmark::microbenchmark(
info_bench cat = cat_info(),
log4r = log4r_info(),
futile.logger = fl_info(),
logging = logging_info(),
logger = logger_info(),
lgr = lgr_info(),
loggit = loggit_info(),
rlog = rlog_info(),
times = 500,
control = list(warmups = 50)
)
<- microbenchmark::microbenchmark(
debug_bench cat = cat_debug(),
log4r = log4r_debug(),
futile.logger = fl_debug(),
logging = logging_debug(),
logger = logger_debug(),
lgr = lgr_debug(),
rlog = rlog_debug(),
times = 500,
control = list(warmups = 50)
)
print(info_bench, order = "median")
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> cat 28.4 38.20 53.3370 41.20 52.50 2734.9 500
#> log4r 27.0 51.05 60.3632 59.15 66.15 1114.5 500
#> rlog 62.0 96.45 115.8454 107.65 120.05 3002.2 500
#> logger 225.4 279.45 305.2482 293.25 306.65 3250.3 500
#> futile.logger 325.6 368.45 395.0182 380.85 395.35 3326.8 500
#> logging 332.7 374.30 412.3304 387.80 407.20 7940.0 500
#> lgr 583.6 620.05 718.6816 635.60 654.15 32433.0 500
#> loggit 760.6 792.45 864.7798 809.95 833.50 7186.6 500
print(debug_bench, order = "median")
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> cat 1.6 2.80 5.7660 4.25 5.40 810.6 500
#> log4r 8.6 10.80 16.4444 13.15 17.10 1203.8 500
#> rlog 11.7 15.00 21.0598 20.80 23.60 844.6 500
#> lgr 14.2 18.25 25.8534 24.70 29.70 897.8 500
#> logger 23.0 28.90 38.2356 39.60 43.50 845.4 500
#> logging 21.9 28.80 46.8470 41.40 48.40 3209.9 500
#> futile.logger 229.1 251.90 281.4642 266.95 279.35 3489.4 500