| 1 |
context("utils-logging") |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
test_that("log suppression", { |
| 7 |
lg <- get_logger("test") |
| 8 |
|
| 9 |
# without_logging() suppresses log messages temporarily |
| 10 |
expect_output(lg$fatal("test"), "FATAL") |
| 11 |
expect_silent(without_logging(lg$fatal("test"))) |
| 12 |
expect_output(lg$fatal("test"), "FATAL") |
| 13 |
|
| 14 |
# suspending works and can be ignored with with_logging() |
| 15 |
suspend_logging() |
| 16 |
expect_silent(lg$fatal("test")) |
| 17 |
expect_output(with_logging(lg$fatal("test"))) |
| 18 |
|
| 19 |
# unsuspending logging works |
| 20 |
unsuspend_logging() |
| 21 |
expect_output(lg$fatal("test"), "FATAL") |
| 22 |
}) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
test_that("without_logging does not conflict with suspend/unsuspend logging", { |
| 28 |
suspend_logging() |
| 29 |
without_logging("blah") |
| 30 |
expect_identical(getOption("lgr.logging_suspended"), TRUE) |
| 31 |
unsuspend_logging() |
| 32 |
}) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
test_that("with_log_level works", { |
| 38 |
lg <- get_logger("test") |
| 39 |
lg$ |
| 40 |
set_appenders(AppenderConsole$new(threshold = "info"))$ |
| 41 |
set_threshold(NA)$ |
| 42 |
set_propagate(FALSE) |
| 43 |
|
| 44 |
|
| 45 |
expect_output( |
| 46 |
with_log_level("warn", lg$info("blubb"), logger = lg), |
| 47 |
"WARN" |
| 48 |
) |
| 49 |
|
| 50 |
foo <- function(){ |
| 51 |
with_log_level("trace", lg$info("blubb"), logger = lg) |
| 52 |
} |
| 53 |
expect_silent(foo()) |
| 54 |
}) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
test_that("with_log_level works", { |
| 59 |
lg <- Logger$new( |
| 60 |
"test", |
| 61 |
appenders = AppenderConsole$new(layout = LayoutJson$new()), |
| 62 |
propagate = FALSE |
| 63 |
) |
| 64 |
|
| 65 |
expect_output( |
| 66 |
with_log_value(list(foo = "bar", a = 1:2), lg$info("blubb"), logger = lg), |
| 67 |
'"a":\\[1,2\\],"foo":"bar"' |
| 68 |
) |
| 69 |
|
| 70 |
foo <- function(){ |
| 71 |
with_log_value(list(foo = "bar"), lg$info("blubb"), logger = lg) |
| 72 |
} |
| 73 |
expect_output(foo()) |
| 74 |
}) |
| 75 |
|