|
1 | 1 | context("Logger") |
2 | 2 |
|
3 | 3 |
|
| 4 | +# Logger ------------------------------------------------------------------ |
| 5 | + |
| 6 | + |
4 | 7 |
|
5 | 8 | test_that("logging conditions works", { |
6 | 9 | e <- error("blahblah") |
@@ -379,11 +382,17 @@ test_that("$config works with lists", { |
379 | 382 |
|
380 | 383 |
|
381 | 384 |
|
| 385 | +# Multi-Logger tests ------------------------------------------------------------- |
382 | 386 |
|
383 | 387 | test_that("Logger$log() dispatches to all appenders, even if some throw an error", { |
384 | 388 | ln <- Logger$new("normal", propagate = FALSE) |
385 | 389 | lg <- LoggerGlue$new("glue", propagate = FALSE) |
386 | 390 |
|
| 391 | + on.exit({ |
| 392 | + ln$config(NULL) |
| 393 | + lg$config(NULL) |
| 394 | + }) |
| 395 | + |
387 | 396 | AppErr <- R6::R6Class( |
388 | 397 | inherit = AppenderConsole, |
389 | 398 | public = list( |
@@ -416,6 +425,11 @@ test_that("Logger error contains useful call object", { |
416 | 425 | l <- get_logger("test") |
417 | 426 | g <- get_logger_glue("testglue") |
418 | 427 |
|
| 428 | + on.exit({ |
| 429 | + l$config(NULL) |
| 430 | + g$config(NULL) |
| 431 | + }) |
| 432 | + |
419 | 433 | expect_warning(l$info("this will fail", e = stop()), "l\\$info") |
420 | 434 | expect_warning(g$info("this will fail", e = stop()), "g\\$info") |
421 | 435 | }) |
@@ -451,18 +465,52 @@ test_that("Appender error contains useful call object", { |
451 | 465 | }) |
452 | 466 |
|
453 | 467 |
|
| 468 | +test_that("Logger$log - with 'msg' argument - logs the message", { |
| 469 | + l <- get_logger("test")$set_propagate(FALSE) |
| 470 | + |
| 471 | + on.exit({ |
| 472 | + l$config(NULL) |
| 473 | + }) |
| 474 | + |
| 475 | + expect_silent(l$log(level = "fatal", msg = "test")) |
| 476 | +}) |
454 | 477 |
|
455 | 478 |
|
| 479 | +test_that("LoggerGlue$log - with 'msg' argument - throws a warning", { |
| 480 | + l <- get_logger_glue("testglue")$set_propagate(FALSE) |
456 | 481 |
|
457 | | -test_that("Logger$log 'msg' argument works as expected", { |
| 482 | + on.exit({ |
| 483 | + l$config(NULL) |
| 484 | + }) |
| 485 | + |
| 486 | + expect_warning(l$log(level = "fatal", msg = "test"), "does not support") |
| 487 | +}) |
| 488 | + |
| 489 | + |
| 490 | +test_that("Logger$log - with string interpolation - adds .rawMsg to event", { |
458 | 491 | l <- get_logger("test")$set_propagate(FALSE) |
459 | | - g <- get_logger_glue("testglue")$set_propagate(FALSE) |
460 | 492 |
|
461 | 493 | on.exit({ |
462 | 494 | l$config(NULL) |
463 | | - g$config(NULL) |
464 | 495 | }) |
465 | 496 |
|
466 | | - expect_silent(l$log(level = "fatal", msg = "test")) |
467 | | - expect_warning(g$log(level = "fatal", msg = "test"), "does not support") |
| 497 | + l$info("foo %s", "bar") |
| 498 | + |
| 499 | + expect_identical(l$last_event$msg, "foo bar") |
| 500 | + expect_identical(l$last_event$.rawMsg, "foo %s") |
468 | 501 | }) |
| 502 | + |
| 503 | + |
| 504 | +test_that("LoggerGlue$log - with string interpolation - adds .rawMsg to event", { |
| 505 | + l <- get_logger_glue("testglue")$set_propagate(FALSE) |
| 506 | + |
| 507 | + on.exit({ |
| 508 | + l$config(NULL) |
| 509 | + }) |
| 510 | + |
| 511 | + l$fatal("hash {x}", x = "baz") |
| 512 | + |
| 513 | + expect_identical(as.character(l$last_event$msg), "hash baz") |
| 514 | + expect_identical(l$last_event$.rawMsg, "hash {x}") |
| 515 | +}) |
| 516 | + |
0 commit comments