Debian Codegraph @

inst/logger_comparison/logger_comparison.Rmd

raw
1 ---
2 title: "Logger Comparison"
3 author: "Stefan Fleck"
4 date: "`r Sys.Date()`"
5 output:
6 html_document:
7 toc: true
8 number_sections: true
9 ---
10
11 ```{r setup, include = FALSE}
12 knitr::opts_chunk$set(
13 collapse = TRUE,
14 comment = "#>"
15 )
16
17 library(devtools)
18 library(bench)
19 library(dplyr)
20 library(forcats)
21
22 ```
23
24 ```{r eval = FALSE}
25 install_github("daroczig/logger")
26 install.packages("log4r")
27 install.packages("futile.logger")
28 install.packages("logging")
29 ```
30
31 ```{r}
32 library(lgr)
33 library(logger)
34 library(log4r)
35 library(futile.logger)
36 ```
37
38
39
40 Setup packages that do not work out-of-the-box
41 ```{r}
42 logging::addHandler(logging::writeToConsole, logger="logging")
43
44 lgr::lgr$info("an informative message")
45 logging::loginfo("an informative message")
46 futile.logger::flog.info("an informative message")
47
48 ```
49
50 # Syntax Examples
51
52 wip
53
54 # Feature Matrix
55
56 wip
57
58
59 # Performance
60
61 This section reviews the performance of available logging packages against
62 `cat()`.
63
64 ## Simple Console Logging Call
65
66 Log a simple log message
67
68 ```{r, results='hide'}
69 # By default lgr is configured for color output, so we disable that for a fair
70 # comparison
71 lgr$set_appenders(AppenderConsole$new(layout = LayoutFormat$new()))
72 sink("/dev/null")
73 res <- bench::mark(
74 cat = cat("FATAL", " [", format(Sys.time()), "] ", "test", sep = ""),
75 logger = logger::log_info("test"),
76 futile.logger = futile.logger::flog.info("test"),
77 logging = logging::logerror("test", logger = "logging"),
78 lgr = lgr::lgr$info("test"),
79 check = FALSE
80 )
81 sink()
82 ```
83 ```{r}
84 res %>%
85 select(expression, min, mean, median, mem_alloc) %>%
86 arrange(median) %>%
87 knitr::kable()
88
89 res$expression <- fct_reorder(res$expression, res$median)
90
91 plot(res)
92
93 ```
94
95 For simple log messages **logging* and *logger** are in the clear lead
96 performance wise
97
98
99 # Log a simple log message (with colors)
100
101 The only packages that support color output are **lgr** and **logger**. Both
102 do not allow much configuration of the color output. Here lgr is in the lead,
103 mainly because **logger** relies on **glue** for the color formatting. Both
104
105
106 ```{r, results='hide'}
107 lgr$set_appenders(AppenderConsole$new())
108 log_layout(layout_glue_colors)
109 library(crayon)
110
111 sink("/dev/null")
112 res <- bench::mark(
113 cat = cat(red("FATAL"), " [", format(Sys.time()), "] ", "test", sep = ""),
114 logger = logger::log_fatal("test"),
115 lgr = lgr::lgr$info("test"),
116 check = FALSE
117 )
118 sink()
119 ```
120
121 ```{r}
122 res %>%
123 select(expression, min, mean, median, mem_alloc) %>%
124 arrange(median) %>%
125 knitr::kable()
126
127 res$expression <- fct_reorder(res$expression, res$median)
128
129 plot(res)
130
131 ```
132
133
134 # Log a suppressed message
135
136
137
138 ```{r}
139 lgr$set_threshold("info")
140
141 res <- bench::mark(
142 cat = cat(),
143 logger = logger::log_debug("test"),
144 futile.logger = futile.logger::flog.debug("test"),
145 logging = logging::logdebug("test", logger = "logging"),
146 lgr = lgr::lgr$debug("test"),
147 check = FALSE
148 )
149
150 ```
151
152 ```{r}
153 plot(res)
154 ```
155
156 Here lgr is pretty much in the lead, because it completely disables the
157 logging functions that are under the loggers threshold.
158
159
160 ### Conclusion
161
162 While there are subtle differences in the performance of the available logging
163 packages, all perform pretty well. Nothing can compare with a simple `cat()`,
164 but that was to be expected.