Debian Codegraph @

tests/testthat/test_Logger.R

raw
1 context("Logger")
2
3
4 # Logger ------------------------------------------------------------------
5
6
7
8 test_that("logging conditions works", {
9 e <- error("blahblah")
10
11 ln <- get_logger("test")$
12 config(NULL)$
13 set_propagate(FALSE)
14
15 lg <- get_logger_glue("test_glue")$
16 config(NULL)$
17 set_propagate(FALSE)
18
19 expect_match(ln$fatal(e), "blahblah")
20 expect_match(lg$fatal(e, "this is <{it}>", e, it = 1, conditon = e), "blahblahthis is <1>blahblah")
21 })
22
23
24
25 test_that("set_threshold()", {
26 ml <- Logger$new("test_logger")
27
28 expect_silent(ml$set_threshold(5))
29 expect_identical(ml$threshold, 5L)
30 expect_silent(ml$set_threshold("fatal"))
31 expect_identical(ml$threshold, 100L)
32 expect_error(ml$set_threshold("blubb"), "fatal.*trace")
33
34 walk(ml$appenders, function(.x) expect_true(inherits(.x, "Appender")))
35 })
36
37
38
39
40 test_that("$log() & co work", {
41 ml <- Logger$new("test_logger", appenders = list(memory = AppenderBuffer$new()))
42 ts <- structure(1540486764.41946, class = c("POSIXct", "POSIXt"))
43
44 testfun <- function(){
45 ml$fatal("blubb")
46 ml$log(msg = "testfun", level = 500)
47 }
48
49 expect_output({
50 testfun()
51 testfun()
52 })
53
54 expect_true(
55 all(ml$appenders$memory$data$caller == "testfun"),
56 info = ml$appenders$memory$data$caller
57 )
58 })
59
60
61
62
63 test_that("string formatting works", {
64 l <- Logger$new("test_logger", propagate = FALSE)
65
66 expect_identical(l$fatal("test"), "test")
67 expect_identical(l$fatal("te%s", "st"), "test")
68 expect_identical(l$fatal("te%s", "st", blah = "blubb"), "test")
69 })
70
71
72
73
74 test_that("string formatting is only used if unnamed arguments exist", {
75 lg <- Logger$new(
76 "test",
77 appenders = list(AppenderConsole$new()),
78 propagate = FALSE
79 )
80
81 # unnamed arguments trigger sprintf
82 expect_output(lg$fatal("foo %s", "bar"), "foo bar")
83 expect_output(lg$fatal("foo %s", "bar", fizz = "buzz"), "foo bar")
84
85 # otherwise msg is processed as-is
86 expect_output(lg$fatal("foo %s"), "foo %s")
87 expect_output(lg$fatal("foo %s", fizz = "buzz"), "foo %s")
88 })
89
90
91
92
93 test_that("set_threshold() works for Loggers and Appenders", {
94 lg <- Logger$new("dummy", appenders = AppenderConsole$new())
95 lg$set_threshold(200)
96 expect_identical(lg$threshold, 200L)
97 lg$set_threshold("info")
98 expect_identical(lg$threshold, 400L)
99 lg$set_threshold(NA)
100 expect_identical(lg$threshold, NA_integer_)
101 expect_error(lg$set_threshold("blubb"), "log levels")
102
103 # test if setting for appenders of a logger also works as this is somewhat tricky
104 lg$appenders[[1]]$set_threshold(NA)
105 expect_identical(lg$appenders[[1]]$threshold, NA_integer_)
106 lg$appenders[[1]]$set_threshold(300)
107 expect_identical(lg$appenders[[1]]$threshold, 300L)
108 lg$appenders[[1]]$set_threshold("info")
109 expect_identical(lg$appenders[[1]]$threshold, 400L)
110 expect_error(lg$appenders[[1]]$set_threshold("blubb"), "log levels")
111 })
112
113
114
115
116 test_that("threshold of `0` suspends logging", {
117 ml <- Logger$new("test_logger")
118
119 expect_output(ml$info("blubb"), "blubb")
120
121 expect_output(ml$fatal("blubb"), "FATAL")
122 x <- capture.output(ml$fatal("blubb"))
123 ml$set_threshold(0)
124 expect_identical(ml$fatal("blubb %s", "blah"), NULL)
125 ml$set_threshold("error")
126 y <- capture.output(ml$fatal("blubb"))
127
128 expect_output(ml$log(100, "test"), "FATAL")
129 expect_output(ml$log("error", "test"), "ERROR")
130 expect_silent(ml$warn("blubb"))
131 expect_silent(ml$log(300, "blubb"))
132 # ignore timestamp for comparison
133 x <- gsub("[.*]", x, "[time]")
134 y <- gsub("[.*]", x, "[time]")
135
136 expect_identical(x, y)
137 })
138
139
140
141
142 test_that("add_appenders()/remove_appenders()", {
143 tf <- tempfile()
144 on.exit(unlink(tf))
145 ml <- Logger$new("test_logger", appenders = AppenderFile$new(file = tf))
146 app1 <- AppenderConsole$new(threshold = 100)
147 app2 <- AppenderConsole$new(threshold = 300)
148
149 # add
150 ml$add_appender(app1)
151 ml$add_appender(app2, "blah")
152 ml$add_appender(AppenderBuffer$new(), "blubb")
153 expect_identical(ml$appenders[[2]], app1)
154 expect_identical(ml$appenders$blah, app2)
155
156 # remove
157 ml$remove_appender(2)
158 expect_length(ml$appenders, 3)
159 ml$remove_appender(c("blah", "blubb"))
160 expect_length(ml$appenders, 1L)
161
162 # set
163 ml$set_appenders(list(app1, app2))
164 expect_length(ml$appenders, 2)
165 ml$set_appenders(list(app1, app2))
166 expect_length(ml$appenders, 2)
167 expect_identical(ml$appenders[[1]], app1)
168 expect_identical(ml$appenders[[2]], app2)
169 })
170
171
172
173
174 test_that("setting Appender properties works", {
175 ml <- Logger$new("test_logger", appenders = list(AppenderConsole$new()), propagate = FALSE)
176 tf <- tempfile()
177 on.exit(unlink(tf))
178
179 # Add a new appender to a logger. We don't have to supply a name, but that
180 # mak1es it easier to remove later.
181 ml$add_appender(AppenderFile$new(file = tf), name = "file")
182
183 # configure lgr so that it logs everything to the file, but only info and above
184 # to the console
185 ml$set_threshold(NA)
186 ml$appenders[[1]]$set_threshold("info")
187 ml$appenders$file$set_threshold(NA)
188 expect_output(ml$info("Another informational message"))
189 expect_silent(ml$debug("A debug message that the console appender doesn't show."))
190 expect_identical(length(readLines(tf)), 2L)
191 expect_match(paste(readLines(tf), collapse = "---"), "INFO.*---DEBUG.*")
192 })
193
194
195
196
197 test_that("Exceptions are cought and turned into warnings", {
198 tf <- tempfile()
199 on.exit(unlink(tf))
200 ml <- Logger$new("test_logger",
201 appenders = list(
202 AppenderFile$new(file = tf),
203 AppenderConsole$new()
204 )
205 )
206
207 expect_warning(ml$fatal(stop("blubb")), "error.*blubb")
208 expect_warning(ml$fatal(), "error")
209 })
210
211
212
213
214 test_that("Inheritance and event propagation works", {
215 on.exit({
216 c1$config(NULL)
217 c2$config(NULL)
218 c3$config(NULL)
219 unlink(c(tf1, tf2, tf3))
220 })
221
222 tf1 <- tempfile()
223 tf2 <- tempfile()
224 tf3 <- tempfile()
225
226 c1 <- get_logger("c1")
227 c1$add_appender(AppenderFile$new(tf1))
228
229 c2 <- get_logger("c1/c2")
230 c2$add_appender(AppenderFile$new(tf2))
231
232 c3 <- Logger$new("c1/c2/c3")
233 c3$add_appender(AppenderFile$new(tf3))
234
235 expect_output(c3$fatal("blubb"), "FATAL.*blubb")
236 expect_match(readLines(tf1), "FATAL.*blubb")
237 expect_identical(readLines(tf1), readLines(tf2))
238 expect_identical(readLines(tf1), readLines(tf3))
239
240 c2$set_propagate(FALSE)
241 expect_silent(c3$error("blubb"))
242 expect_identical(readLines(tf2), readLines(tf3))
243 expect_lt(length(readLines(tf1)), length(readLines(tf3)))
244 })
245
246
247
248
249 test_that("Named inherited appenders are displayed correctly", {
250 on.exit({
251 c1$config(NULL)
252 c2$config(NULL)
253 c3$config(NULL)
254 })
255
256 c1 <- get_logger("c1")$
257 set_propagate(FALSE)$
258 add_appender(AppenderConsole$new(), "console")
259
260 expect_null(c1$inherited_appenders)
261
262 c2 <- get_logger("c1/c2")$
263 add_appender(AppenderConsole$new(), "console")
264
265 expect_length(c2$inherited_appenders, 1L)
266
267 c3 <- Logger$new("c1/c2/c3")$
268 add_appender(AppenderConsole$new(), "console")
269
270 expect_length(c3$inherited_appenders, 2L)
271
272 expect_identical(names(c3$inherited_appenders), c("console", "console"))
273 expect_output(print(c3), "\\sconsole:.*\\sconsole:.*")
274 })
275
276
277
278
279 test_that("Unnamed inherited appenders are displayed correctly", {
280 on.exit({
281 c1$config(NULL)
282 c2$config(NULL)
283 c3$config(NULL)
284 })
285
286 c1 <- get_logger("c1")$
287 set_propagate(FALSE)$
288 add_appender(AppenderConsole$new())
289
290 c2 <- get_logger("c1/c2")$
291 add_appender(AppenderConsole$new())
292
293 c3 <- Logger$new("c1/c2/c3")$
294 add_appender(AppenderConsole$new())
295
296 expect_null(names(c3$inherited_appenders))
297 expect_output(print(c3), "\\[\\[1\\]\\]\\:.*\\[\\[2\\]\\]\\:.*")
298 })
299
300
301
302
303 test_that("threshold works", {
304 c1 <- Logger$new("c1")
305 expect_output(c1$error("blubb"), "ERROR")
306 c1$set_threshold(100)
307 expect_silent(c1$error("blubb"))
308 })
309
310
311
312
313 test_that("$ancestry works", {
314 l4 <- get_logger("l1/l2/l3/l4")
315 l2 <- get_logger("l1/l2")$set_propagate(FALSE)
316
317 expect_equal(
318 unname(unclass(l4$ancestry)),
319 c(TRUE, FALSE, TRUE, TRUE)
320 )
321 })
322
323
324 test_that("Logger handles null values", {
325 l <- Logger$new("test", propagate = FALSE)
326
327 log_null <- l$info("foo %s %s", NULL, character())
328 expect_equal(log_null, "foo <NULL> <NULL>")
329 })
330
331
332 test_that("Logger handles null values along with named parameters", {
333 l <- Logger$new("test", propagate = FALSE)
334
335 log_null <- l$info("foo %s %s", NULL, character(), valbert = "hashbaz", nullbert = NULL)
336 expect_equal(log_null, "foo <NULL> <NULL>")
337
338 expect_true(l$last_event$valbert == "hashbaz")
339 expect_true("nullbert" %in% names(l$last_event$values))
340 expect_null(l$last_event$nullbert)
341 })
342
343
344 # LoggerGlue --------------------------------------------------------------
345
346 test_that("LoggerGlue supports custom fields", {
347 l <- LoggerGlue$new("glue")
348
349 expect_output(l$fatal("test", "test"), glue::glue("test", "test"))
350
351 expect_output(
352 l$fatal("blah", "blubb", foo = "bar"),
353 ".*blahblubb.*bar.*\\}"
354 )
355 expect_output(
356 l$fatal("blah", "blubb {fizz}", foo = "bar", fizz = "buzz"),
357 "buzz.*foo.*bar.*fizz.*buzz.*"
358 )
359
360 expect_output(l$fatal("a", .open = "{", .foo = "bar", fizz = "buzz"))
361
362 expect_true("fizz" %in% names(l$last_event))
363 expect_false(".open" %in% names(l$last_event))
364 expect_false(".foo" %in% names(l$last_event))
365 })
366
367
368 test_that("LoggerGlue uses the correct evaluation environment", {
369 l <- LoggerGlue$new("glue", propagate = FALSE)
370
371 expect_match(l$fatal("{iris[['Species']][[1]]}"), "setosa")
372 expect_match(l$log(100, "{iris[['Species']][[1]]}"), "setosa")
373 expect_match(l$fatal(100, "{x}", x = iris[['Species']][[1]]), "setosa")
374 expect_match(l$log(100, "{x}", x = iris[['Species']][[1]]), "setosa")
375 })
376
377
378 test_that("$config works with lists", {
379 l <- LoggerGlue$new("glue", propagate = FALSE)
380
381 cfg <- list(
382 appenders = list(blubb = AppenderConsole$new()),
383 propagate = FALSE
384 )
385
386 expect_identical(names(l$config(cfg)$appenders), "blubb")
387 expect_identical(names(l$config(list = cfg)$appenders), "blubb")
388 expect_identical(l$config(cfg)$propagate, FALSE)
389 expect_error(l$config(cfg = cfg, list = cfg))
390
391 l$config(NULL)
392 expect_true(is_virgin_Logger(l, allow_subclass = TRUE))
393 expect_false(is_virgin_Logger(l))
394 })
395
396
397 test_that("LoggerGlue handles null values", {
398 l <- LoggerGlue$new("glue", propagate = FALSE)
399
400 log_null <- l$info("foo", NULL)
401 expect_equal(log_null, "foo<NULL>")
402
403 log_null_parameter <- l$info("foo {bar} {baz}", bar = NULL, baz = character())
404 expect_equal(log_null_parameter, "foo <NULL> <NULL>")
405 })
406
407
408 test_that("LoggerGlue handles null values along with named parameters", {
409 l <- LoggerGlue$new("test", propagate = FALSE)
410
411 log_null <- l$info("foo ", NULL, character(), valbert = "hashbaz", nullbert = NULL)
412 expect_equal(log_null, "foo <NULL><NULL>")
413
414 expect_true(l$last_event$valbert == "hashbaz")
415 expect_true("nullbert" %in% names(l$last_event$values))
416 expect_null(l$last_event$nullbert)
417 })
418
419
420 test_that("Logger glue can use custom transformers", {
421 transformer <- function(text, envir) {
422 "transformed text"
423 }
424
425 l <- LoggerGlue$new("glue", propagate = FALSE, transformer = transformer)
426
427 e <- log_null <- l$info("foo {bar}", foo = "bar")
428
429 expect_equal(e, "foo transformed text")
430 })
431
432
433 # Multi-Logger tests -------------------------------------------------------------
434
435 test_that("Logger$log() dispatches to all appenders, even if some throw an error", {
436 ln <- Logger$new("normal", propagate = FALSE)
437 lg <- LoggerGlue$new("glue", propagate = FALSE)
438
439 on.exit({
440 ln$config(NULL)
441 lg$config(NULL)
442 })
443
444 AppErr <- R6::R6Class(
445 inherit = AppenderConsole,
446 public = list(
447 append = function(...) stop("error")
448 )
449 )
450
451 tf <- tempfile()
452 on.exit(unlink(tf))
453
454 ln$set_appenders(list(
455 err = AppErr$new(),
456 file = AppenderFile$new(file = tf)
457 ))
458 lg$set_appenders(list(
459 err = AppErr$new(),
460 file = AppenderFile$new(file = tf)
461 ))
462
463 expect_warning(ln$info("test_normal"), class = "appendingFailedWarning")
464 expect_warning(ln$info("test_glue"), class = "appendingFailedWarning")
465
466 expect_true(any(grepl("test_normal", readLines(tf))))
467 expect_true(any(grepl("test_glue", readLines(tf))))
468 })
469
470
471
472 test_that("Logger error contains useful call object", {
473 l <- get_logger("test")
474 g <- get_logger_glue("testglue")
475
476 on.exit({
477 l$config(NULL)
478 g$config(NULL)
479 })
480
481 expect_warning(l$info("this will fail", e = stop()), "l\\$info")
482 expect_warning(g$info("this will fail", e = stop()), "g\\$info")
483 })
484
485
486
487
488
489 test_that("Appender error contains useful call object", {
490 l <- get_logger("test")$set_propagate(FALSE)
491 g <- get_logger_glue("testglue")$set_propagate(FALSE)
492
493 on.exit({
494 l$config(NULL)
495 g$config(NULL)
496 })
497
498 AppenderFail <- R6::R6Class(
499 "AppenderFail",
500 inherit = Appender,
501 public = list(
502 append = function(e) stop("bummer")
503 )
504 )
505
506 a <- AppenderFail$new()
507
508 l$add_appender(a, "fail")
509 g$add_appender(a, "fail")
510
511 expect_warning(l$info("this will fail"), ".*AppenderFail.*l\\$info")
512 expect_warning(g$info("this will fail"), ".*AppenderFail.*g\\$info")
513 })
514
515
516 test_that("Logger$log - with 'msg' argument - logs the message", {
517 l <- get_logger("test")$set_propagate(FALSE)
518
519 on.exit({
520 l$config(NULL)
521 })
522
523 expect_silent(l$log(level = "fatal", msg = "test"))
524 })
525
526
527 test_that("LoggerGlue$log - with 'msg' argument - throws a warning", {
528 l <- get_logger_glue("testglue")$set_propagate(FALSE)
529
530 on.exit({
531 l$config(NULL)
532 })
533
534 expect_warning(l$log(level = "fatal", msg = "test"), "does not support")
535 })
536
537
538 test_that("Logger$log - with string interpolation - adds rawMsg to event", {
539 l <- get_logger("test")$set_propagate(FALSE)
540
541 on.exit({
542 l$config(NULL)
543 })
544
545 l$info("foo %s", "bar")
546
547 expect_identical(l$last_event$msg, "foo bar")
548 expect_identical(l$last_event$rawMsg, "foo %s")
549 })
550
551
552 test_that("LoggerGlue$log - with string interpolation - adds rawMsg to event", {
553 l <- get_logger_glue("testglue")$set_propagate(FALSE)
554
555 on.exit({
556 l$config(NULL)
557 })
558
559 l$fatal("hash {x}", x = "baz")
560
561 expect_identical(as.character(l$last_event$msg), "hash baz")
562 expect_identical(l$last_event$rawMsg, "hash {x}")
563 })
564