Skip to content

Commit 3732649

Browse files
author
Stefan Fleck
committed
added with_logging() for symmetry with without_logging()
1 parent 28ff6ae commit 3732649

File tree

7 files changed

+125
-74
lines changed

7 files changed

+125
-74
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,6 @@ export(unsuspend_logging)
8080
export(use_logger)
8181
export(with_log_level)
8282
export(with_log_value)
83+
export(with_logging)
8384
export(without_logging)
8485
importFrom(stats,setNames)

NEWS.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# lgr 0.1.1.9000
1+
# lgr 0.2.0.9000
2+
3+
* to be released as 1.0.0
24

35
* `get_loggers()` registeres new loggers in the lgr::loggers namespace, this
46
is a more global and decoupled approach to loggers.
@@ -9,8 +11,12 @@
911
For consistency the format method of `ancestry` has also been revised.
1012
* New Loggers now inherit the log level of their parent unless one is set
1113
* added a `config` method for Loggers.
12-
* Depend on R6 >= 2.4.0 (which includes some relevant fixes to finalizers)
14+
* Depend on R6 >= 2.4.0 (which includes relevant fixes to finalizers)
1315
* finalize methods are now private
16+
* support for new ways to configure loggers, including YAML config files
17+
(experimental)
18+
* added `with_logging()`, the opposite of `without_logging()`. This can be
19+
handy for automated tests
1420

1521

1622
# lgr 0.1.1

R/utils-logging.R

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
#' Suspend All Logging
22
#'
33
#' Completely disable logging for all loggers. This is for example useful for
4-
#' automated test code.
4+
#' automated test code. `suspend_logging()` globally disables all logging with
5+
#' lgr until `unsuspend_logging()` is invoked, while `without_logging()` and
6+
#' `with_logging()` temporarily disable/enable logging.
57
#'
68
#' @return
79
#' `suspend_logging()` and `unsuspend_logging()` return `NULL` (invisibly),
8-
#' `without_logging` returns whatever `code` returns
10+
#' `without_logging()` and `with_logging()` returns whatever `code` returns.
911
#' @export
12+
#' @examples
13+
#' lg <- get_logger("test")
14+
#'
15+
#' # temporarily disable logging
16+
#' lg$fatal("foo")
17+
#' without_logging(lg$fatal("bar"))
18+
#'
19+
#' # globally disable logging
20+
#' suspend_logging()
21+
#' lg$fatal("bar")
22+
#' with_logging(lg$fatal("foo")) # log anyways
23+
#'
24+
#' # globally enable logging again
25+
#' unsuspend_logging()
26+
#' lg$fatal("foo")
1027
suspend_logging <- function(){
1128
options("lgr.logging_suspended" = TRUE)
1229
invisible()
@@ -29,10 +46,6 @@ unsuspend_logging <- function(){
2946
#' @param code Any \R code
3047
#' @export
3148
#' @examples
32-
#' without_logging({
33-
#' FATAL("FOO")
34-
#' INFO("BAR")
35-
#' })
3649
#'
3750
without_logging <- function(code){
3851
old <- getOption("lgr.logging_suspended")
@@ -44,6 +57,25 @@ without_logging <- function(code){
4457

4558

4659

60+
61+
#'
62+
#'
63+
#' @rdname suspend_logging
64+
#' @param code Any \R code
65+
#' @export
66+
#' @examples
67+
#'
68+
with_logging <- function(code){
69+
old <- getOption("lgr.logging_suspended")
70+
on.exit(options(lgr.logging_suspended = old))
71+
unsuspend_logging()
72+
force(code)
73+
}
74+
75+
76+
77+
78+
4779
#' Inject Values into Logging Calls
4880
#'
4981
#' `with_log_level` temporarily overrides the log level of all [LogEvents]

man/as_logger_config.Rd

Lines changed: 0 additions & 52 deletions
This file was deleted.

man/logger_config.Rd

Lines changed: 42 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/suspend_logging.Rd

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_utils-logging.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ context("utils-logging")
44

55

66
test_that("log suppression", {
7-
expect_output(FATAL("test"), "FATAL")
8-
expect_silent(without_logging(FATAL("test")))
9-
expect_output(FATAL("test"), "FATAL")
7+
lg <- get_logger("test")
108

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()
1115
suspend_logging()
12-
expect_silent(FATAL("test"))
16+
expect_silent(lg$fatal("test"))
17+
expect_output(with_logging(lg$fatal("test")))
18+
19+
# unsuspending logging works
1320
unsuspend_logging()
14-
expect_output(FATAL("test"), "FATAL")
21+
expect_output(lg$fatal("test"), "FATAL")
1522
})
1623

1724

0 commit comments

Comments
 (0)