Debian Codegraph @

tests/testthat/test_get_logger.R

raw
1 context("get_logger")
2
3
4
5
6 test_that("get_logger(): works as expected", {
7
8 lg <- get_logger("blubb")
9
10 expect_identical(lg$name, "blubb")
11 expect_identical(lg$name, "blubb")
12 expect_identical(lg$parent, lgr)
13
14 lg1 <- get_logger("fizz")
15 lg2 <- get_logger("fizz/buzz")
16 lg3 <- get_logger("fizz/buzz/wuzz")
17
18 expect_identical(lg3$parent, lg2)
19 expect_identical(lg2$parent, lg1)
20 expect_identical(lg1$parent, lgr::lgr)
21
22 lg2$set_propagate(FALSE)
23
24 expect_equal(
25 unname(unclass(lg3$ancestry)),
26 c(TRUE, FALSE, TRUE)
27 )
28
29 expect_equal(
30 names(lg3$ancestry),
31 c("fizz", "buzz", "wuzz")
32 )
33 })
34
35
36
37
38 test_that("logger names can be specified using scalars or vectors", {
39 lg1 <- get_logger("fizz/buzz/wuzz")
40 lg2 <- get_logger(c("fizz", "buzz", "wuzz"))
41 expect_identical(lg1, lg2)
42 })
43
44
45
46
47 test_that("get_logger is not confused by existing objects with the same name as logger", {
48 lg1 <- get_logger("log/ger/test")
49 lg2 <- get_logger("log/ger")
50 expect_identical(lg1$parent, lg2)
51 })
52
53
54
55
56 test_that("is_virgin_Logger() identifies loggers without settings", {
57 lg1 <- get_logger("foo/bar")
58 expect_true(is_virgin_Logger("foo/bar"))
59 lg1$set_threshold("off")
60 expect_false(is_virgin_Logger("foo/bar"))
61 lg1$set_threshold(NULL)
62 expect_true(is_virgin_Logger(get_logger("foo/bar")))
63 })
64
65
66
67
68 test_that("get_logger_glue() works as expected", {
69 lg <- get_logger("log/ger/test")
70
71 expect_true(is_Logger(lg))
72 expect_s3_class(get_logger_glue("log/ger"), "LoggerGlue")
73 expect_s3_class(get_logger("log/ger"), "LoggerGlue")
74 expect_identical(lg$parent, get_logger("log/ger"))
75 lg$config(NULL)
76 })
77
78
79
80
81 test_that("get_logger_glue() succeedes to get preconfigured glue loggers", {
82 lg <- get_logger_glue("log/ger/test")
83 lg$set_threshold("fatal")
84 lg <- get_logger_glue("log/ger/test")
85 expect_s3_class(lg, "LoggerGlue")
86 get_logger("log/ger/test", reset = TRUE)
87 })
88
89
90
91
92 test_that("get_logger_glue() fails to get preconfigured loggers that are not glue loggers", {
93 lg <- get_logger("log/ger/test2")
94 lg$set_threshold("fatal")
95 expect_error(get_logger_glue("log/ger/test2"), "LoggerGlue")
96 get_logger("log/ger/test2", reset = TRUE)
97 })
98
99
100
101
102 test_that("get_logger(reset == TRUE) completely resets logger", {
103 lg <- get_logger_glue("log/ger/reset")
104 lg$set_threshold("fatal")
105 lg$add_appender(AppenderConsole$new())
106
107 expect_s3_class(lg, "LoggerGlue")
108 expect_identical(lg$threshold, 100L)
109 expect_identical(lg, get_logger("log/ger/reset"))
110
111 lg2 <- get_logger("log/ger/reset", reset = TRUE)
112 lg1 <- get_logger("log/ger/reset")
113 expect_identical(
114 data.table::address(lg1),
115 data.table::address(lg2)
116 )
117
118 lg_g1 <- get_logger_glue("log/ger/reset")
119 lg_g2 <- get_logger_glue("log/ger/reset")
120 expect_identical(lg1, lg2)
121 expect_identical(
122 data.table::address(lg_g1),
123 data.table::address(lg_g2)
124 )
125 expect_false(inherits(lg2, "LoggerGlue"))
126 expect_identical(lg2$threshold, 400L)
127 })
128
129
130
131
132 test_that("get_logger(reset == TRUE) invalidates old Logger", {
133 lg1 <- get_logger("log/ger/reset", reset = TRUE)
134 lg1$set_threshold("fatal")
135
136 lg2 <- get_logger("log/ger/reset", reset = TRUE)$
137 set_threshold("info")$
138 add_appender(AppenderConsole$new())$
139 set_propagate(FALSE)
140
141 expect_true(!identical(lg1, lg2))
142 expect_warning(lg1$fatal("test"), "log/ger/reset")
143 expect_output(lg2$info("test"))
144
145
146 # invalidation works with logger glue
147 get_logger("log/ger/reset", reset = TRUE)
148 lg1 <- get_logger_glue("log/ger/reset")
149 lg1$set_threshold("fatal")
150
151 lg2 <- get_logger("log/ger/reset", reset = TRUE)
152 expect_true(!identical(lg1, lg2))
153 expect_warning(lg1$fatal("test"), "log/ger/reset")
154 expect_output(lg2$info("test"))
155 get_logger("log/ger/reset", reset = TRUE)
156 })