# Logger Comparison

#### Stefan Fleck

#### 2018-12-24

    install_github("daroczig/logger")
    install.packages("log4r")
    install.packages("futile.logger")
    install.packages("logging")

    library(yog)
    library(logger)
    library(log4r)
    #>
    #> Attaching package: 'log4r'
    #> The following object is masked from 'package:base':
    #>
    #>     debug
    library(futile.logger)
    #>
    #> Attaching package: 'futile.logger'
    #> The following objects are masked from 'package:logger':
    #>
    #>     DEBUG, ERROR, FATAL, INFO, TRACE, WARN

Setup packages that do not work out-of-the-box

    logging::addHandler(logging::writeToConsole, logger="logging")

    yog::yog$info("an informative message")
    #> INFO  [12:22:13.245] an informative message
    logging::loginfo("an informative message")
    futile.logger::flog.info("an informative message")
    #> INFO [2018-12-24 12:22:13] an informative message

# 1 Syntax Examples

# 2 Feature Matrix

# 3 Performance

## 3.1 Simple Console Logging Call

Log a simple log message

    # By default yog is configured for color output, so we disable that for a fair
    # comparison
    yog$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"),
      yog = yog::yog$info("test"),
      check = FALSE
    )
    sink()

    res %>%
      select(expression, min, mean, median, mem_alloc) %>%
      arrange(median) %>%
      knitr::kable()

expression | min | mean | median | mem_alloc
--- | --- | --- | --- | ---
cat | 45.72µs | 153.57µs | 159.85µs | 0B
logger | 199.6µs | 230.03µs | 215.13µs | 174.4KB
logging | 198.35µs | 247.82µs | 217.09µs | 45.3KB
yog | 482.85µs | 608.92µs | 556.07µs | 176.8KB
futile.logger | 2.17ms | 2.69ms | 2.49ms | 89.1KB

    res$expression <- fct_reorder(res$expression, res$median)

    plot(res)

For simple log messages **logging* and *logger** are in the clear lead perfomance wise

# 4 Log a simple log message (with colors)

The only packages that support color output are yog and logger. Both do not allow by default to configure the colouring in detail. Here yog has a clear advantage because logger relies on `glue` for the color format.

    yog$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"),
      yog = yog::yog$info("test"),
      check = FALSE
    )
    sink()

    res %>%
      select(expression, min, mean, median, mem_alloc) %>%
      arrange(median) %>%
      knitr::kable()

expression | min | mean | median | mem_alloc
--- | --- | --- | --- | ---
cat | 106.7µs | 225.31µs | 196.3µs | 10.5KB
yog | 848.63µs | 1.04ms | 943.23µs | 8.09KB
logger | 1.61ms | 1.86ms | 1.75ms | 108.97KB

    res$expression <- fct_reorder(res$expression, res$median)

    plot(res)

# 5 Log a suppressed message

    yog$set_threshold("info")

    res <- bench::mark(
      logger = logger::log_debug("test"),
      futile.logger = futile.logger::flog.debug("test"),
      logging = logging::logdebug("test", logger = "logging"),
      yog = yog::yog$debug("test"),
      check = FALSE
    )

    plot(res)