Debian Codegraph @

R/utils-logging.R

raw
1 #' Suspend All Logging
2 #'
3 #' Completely disable logging for all loggers. This is for example useful for
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.
7 #'
8 #' @return
9 #' `suspend_logging()` and `unsuspend_logging()` return `NULL` (invisibly),
10 #' `without_logging()` and `with_logging()` returns whatever `code` returns.
11 #' @export
12 #' @examples
13 #' lg <- get_logger("test")
14 #'
15 #' # temporarily disable logging
16 #' lg$fatal("foo")
17 #' without_logging({
18 #' lg$info("everything in this codeblock will be suppressed")
19 #' lg$fatal("bar")
20 #' })
21 #'
22 #' # globally disable logging
23 #' suspend_logging()
24 #' lg$fatal("bar")
25 #' with_logging(lg$fatal("foo")) # log anyways
26 #'
27 #' # globally enable logging again
28 #' unsuspend_logging()
29 #' lg$fatal("foo")
30 suspend_logging <- function(){
31 options("lgr.logging_suspended" = TRUE)
32 invisible()
33 }
34
35
36
37
38 #' @rdname suspend_logging
39 #' @export
40 unsuspend_logging <- function(){
41 options("lgr.logging_suspended" = FALSE)
42 invisible()
43 }
44
45
46
47
48 #' @rdname suspend_logging
49 #' @param code Any \R code
50 #' @export
51 without_logging <- function(code){
52 old <- getOption("lgr.logging_suspended")
53 on.exit(options(lgr.logging_suspended = old))
54 suspend_logging()
55 force(code)
56 }
57
58
59
60
61
62 #' @rdname suspend_logging
63 #' @export
64 with_logging <- function(code){
65 old <- getOption("lgr.logging_suspended")
66 on.exit(options(lgr.logging_suspended = old))
67 unsuspend_logging()
68 force(code)
69 }
70
71
72
73
74
75 #' Inject Values into Logging Calls
76 #'
77 #' `with_log_level` temporarily overrides the log level of all [LogEvents]
78 #' created by target [Logger].
79 #'
80 #' These functions abuses lgr's filter mechanic to modify LogEvents in-place
81 #' before they passed on the Appenders. Use with care as they can
82 #' produce hard to reason about code.
83 #'
84 #' @param level `integer` or `character` scalar: the desired log level
85 #' @param code Any \R code
86 #' @param logger a [Logger] or the name of one (see [get_logger()]). Defaults
87 #' to the root logger (`lgr::lgr`).
88 #'
89 #' @return whatever `code` would return
90 #' @export
91 #' @examples
92 #' with_log_level("warn", {
93 #' lgr$info("More important than it seems")
94 #' lgr$fatal("Really not so bad")
95 #' })
96 with_log_level <- function(
97 level,
98 code,
99 logger = lgr::lgr
100 ){
101 if (is_scalar_character(logger)){
102 logger <- get_logger(logger)
103 }
104 assert(is_Logger(logger))
105
106 level <- standardize_log_level(level)
107 force(level)
108
109 set_level <- function(event){
110 event[["level"]] <- level
111 TRUE
112 }
113
114 # to make it unlikely something goes wrong if people do funky stuff with
115 # filters inside with_log_level calls
116 tn <- paste0("...WITH_LOG_LEVEL_TEMP_FILTER", sample.int(1e9, size = 1))
117 logger$add_filter(set_level, name = tn)
118 on.exit(logger$remove_filter(tn))
119
120 force(code)
121 }
122
123
124
125
126 #' `with_log_value()` injects arbitrary values into all [LogEvents] (overriding
127 #' existing ones). This is especially powerful in combination with Appenders
128 #' that support arbitrary log fields, like [AppenderJson].
129 #'
130 #' @param values a named `list` of values to be injected into the logging calls
131 #' @rdname with_log_level
132 #' @export
133 #' @examples
134 #' with_log_value(
135 #' list(msg = "overriden msg"), {
136 #' lgr$info("bar")
137 #' lgr$fatal("FOO")
138 #' })
139 with_log_value <- function(
140 values,
141 code,
142 logger = lgr::lgr
143 ){
144 if (is_scalar_character(logger)){
145 logger <- get_logger(logger)
146 }
147 assert(is_Logger(logger))
148 assert(is_equal_length(names(values), values))
149
150 set_level <- function(event){
151 for (i in seq_along(values)){
152 event[[names(values)[[i]] ]] <- values[[i]]
153 }
154 TRUE
155 }
156
157 tn <- paste0("...WITH_LOG_VALUE_TEMP_FILTER", sample.int(1e9, size = 1))
158 logger$add_filter(set_level, name = tn)
159 on.exit(logger$remove_filter(tn))
160
161 force(code)
162 }