Debian Codegraph @

tests/testthat/test_simple_logging.R

raw
1 context("simple_logging")
2
3 setup({
4 lgr$add_appender(AppenderBuffer$new(threshold = NA), "memory")
5 get_logger("test")$config(NULL)
6 })
7
8 teardown({
9 basic_config()
10 })
11
12
13
14
15 test_that("threshold(), console_threshold() and log_exception() work as expected", {
16 expect_identical(threshold(), lgr$threshold)
17 expect_identical(console_threshold(), lgr$appenders$console$threshold)
18
19 yth <- lgr$threshold
20 cth <- lgr$appenders$console$threshold
21
22 on.exit({
23 lgr$set_threshold(yth)
24 lgr$appenders$console$set_threshold(cth)
25 })
26
27 threshold(NA)
28 console_threshold(NA)
29
30 expect_output(lgr$fatal("test"), "FATAL")
31 expect_output(lgr$trace("test"), "TRACE")
32
33 expect_error(
34 expect_output(log_exception(stop("oops")), "FATAL.*oops$"),
35 "oops"
36 )
37 })
38
39
40
41
42 test_that("show_log()", {
43 expect_output(show_log())
44 expect_true(nrow(show_data()) > 2)
45 expect_output(
46 expect_equal(show_log(), show_log(target = lgr$appenders$memory))
47 )
48 expect_error(show_log(target = lgr$appenders$console), "no method")
49
50 lg <- Logger$new("test", propagate = FALSE)
51 expect_error(show_log(target = lg), "has no Appender")
52 expect_error(show_log(target = iris), "not a Logger or Appender")
53 })
54
55
56
57
58 test_that("add_appender() and remove_appender() work", {
59 tlg <- Logger$new("test", propagate = FALSE)
60
61 add_appender(AppenderConsole$new(), target = tlg)
62 expect_output(tlg$warn("test"), "WARN.*test\\s*$")
63 remove_appender(1, target = tlg)
64 expect_silent(tlg$warn("test"))
65 expect_error(show_log(tlg))
66 })
67
68
69
70
71 test_that("Option 'lgr.log_file' works", {
72 old <- getOption("lgr.log_file")
73 on.exit(options("lgr.log_file" = old))
74
75 options("lgr.log_file" = tempfile())
76
77 res <- default_appenders()
78 expect_identical(length(res), 1L)
79 expect_s3_class(res[[1]], "AppenderFile")
80 expect_s3_class(res[[1]]$layout, "LayoutFormat")
81 expect_true(is.na(res[[1]]$threshold))
82 unlink(getOption("lgr.log_file"))
83
84 options("lgr.log_file" = tempfile(pattern = ".json"))
85 res <- default_appenders()
86 expect_identical(length(res), 1L)
87 expect_s3_class(res[[1]], "AppenderFile")
88 expect_s3_class(res[[1]]$layout, "LayoutJson")
89 expect_true(is.na(res[[1]]$threshold))
90 unlink(getOption("lgr.log_file"))
91
92 options("lgr.log_file" = c(trace = tempfile(pattern = ".json")))
93 res <- default_appenders()
94 expect_identical(length(res), 1L)
95 expect_s3_class(res[[1]], "AppenderFile")
96 expect_s3_class(res[[1]]$layout, "LayoutJson")
97 expect_identical(res[[1]]$threshold, 600L)
98 unlink(getOption("lgr.log_file"))
99
100 options("lgr.log_file" = c(blubb = tempfile('_fail')))
101 expect_warning(res <- default_appenders(), "_fail")
102 unlink(getOption("lgr.log_file"))
103 })
104
105
106
107
108 test_that("show_data() works", {
109 basic_config(memory = TRUE)
110 on.exit(basic_config())
111
112 expect_output({
113 lgr$info("a log message")
114 lgr$info("another message with data", data = 1:10)
115 lgr$info(c("a vectorized message", "blubb"))
116 })
117
118 expect_output(show_log())
119 expect_s3_class(show_data(), "data.frame")
120 expect_identical(nrow(show_data()), 4L)
121 expect_s3_class(show_dt(), "data.table")
122 expect_identical(nrow(show_dt()), 4L)
123 })
124
125
126
127
128 test_that("basic_config can set the console output format", {
129 basic_config(console_fmt = "foobar %L [%t] %c: %m")
130 expect_output(lgr$info("baz"), "foobar")
131 })