knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(devtools) library(bench) library(dplyr) library(forcats)
install_github("daroczig/logger") install.packages("log4r") install.packages("futile.logger") install.packages("logging")
library(lgr) library(logger) library(log4r) library(futile.logger)
Setup packages that do not work out-of-the-box
logging::addHandler(logging::writeToConsole, logger="logging") lgr::lgr$info("an informative message") logging::loginfo("an informative message") futile.logger::flog.info("an informative message")
wip
wip
This section reviews the performance of available logging packages against
cat()
.
Log a simple log message
# By default lgr is configured for color output, so we disable that for a fair # comparison lgr$set_appenders(AppenderConsole$new(layout = LayoutFormat$new())) sink("/dev/null") res <- bench::mark( cat = cat("FATAL", " [", format(Sys.time()), "] ", "test", sep = ""), logger = logger::log_info("test"), futile.logger = futile.logger::flog.info("test"), logging = logging::logerror("test", logger = "logging"), lgr = lgr::lgr$info("test"), check = FALSE ) sink()
res %>% select(expression, min, mean, median, mem_alloc) %>% arrange(median) %>% knitr::kable() res$expression <- fct_reorder(res$expression, res$median) plot(res)
For simple log messages logging and logger are in the clear lead performance wise
The only packages that support color output are lgr and logger. Both do not allow much configuration of the color output. Here lgr is in the lead, mainly because logger relies on glue for the color formatting. Both
lgr$set_appenders(AppenderConsole$new()) log_layout(layout_glue_colors) library(crayon) sink("/dev/null") res <- bench::mark( cat = cat(red("FATAL"), " [", format(Sys.time()), "] ", "test", sep = ""), logger = logger::log_fatal("test"), lgr = lgr::lgr$info("test"), check = FALSE ) sink()
res %>% select(expression, min, mean, median, mem_alloc) %>% arrange(median) %>% knitr::kable() res$expression <- fct_reorder(res$expression, res$median) plot(res)
lgr$set_threshold("info") res <- bench::mark( cat = cat(), logger = logger::log_debug("test"), futile.logger = futile.logger::flog.debug("test"), logging = logging::logdebug("test", logger = "logging"), lgr = lgr::lgr$debug("test"), check = FALSE )
plot(res)
Here lgr is pretty much in the lead, because it completely disables the logging functions that are under the loggers threshold.
While there are subtle differences in the performance of the available logging
packages, all perform pretty well. Nothing can compare with a simple cat()
,
but that was to be expected.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.