Debian Codegraph @

R/logger_config.R

raw
1 #' Logger Configuration Objects
2 #'
3 #' `logger_config()` is an S3 constructor for `logger_config` objects
4 #' that can be passed to the `$config` method of a [Logger]. You
5 #' can just pass a normal `list` instead, but using this constructor is
6 #' a more formal way that includes additional argument checking.
7 #'
8 #'
9 #' @param appenders see [Logger]
10 #' @param threshold see [Logger]
11 #' @param filters see [Logger]
12 #' @param exception_handler see [Logger]
13 #' @param propagate see [Logger]
14 #'
15 #' @return a `list` with the subclass `"logger_config"`
16 #'
17 logger_config <- function(
18 appenders = NULL,
19 threshold = NULL,
20 filters = NULL,
21 exception_handler = NULL,
22 propagate = TRUE
23 ){
24 # init/preconditions
25 if (is.function(exception_handler)){
26 exception_handler <- deparse(exception_handler)
27
28 } else if (!is.null(exception_handler)){
29 assert(
30 is.character(exception_handler),
31 "'exception_handler' must be a function, a character scalar giving the",
32 "name of a function, or a character vector containing arbitrary R code."
33 )
34 }
35
36 assert(is.null(threshold) || is_threshold(threshold))
37 assert(is.null(appenders) || all(vapply(appenders, is.list, logical(1))))
38 assert(is.null(filters) || all(vapply(filters, is.list, logical(1))))
39
40 if (!is.null(propagate)){
41 assert(is_scalar(propagate))
42 if (is.character(propagate)) propagate <- toupper(propagate)
43 propagate <- as.logical(propagate)
44 assert(is_scalar_bool(propagate))
45 }
46
47
48
49 cfg <- compact(list(
50 appenders = appenders,
51 threshold = threshold,
52 filters = filters,
53 exception_handler = exception_handler,
54 propagate = propagate
55 ))
56
57 class(cfg) <- c("logger_config", "list")
58 cfg
59 }
60
61
62
63
64 is_logger_config <- function(x){
65 inherits(x, "logger_config")
66 }
67
68
69
70
71 parsed_logger_config <- function(
72 appenders = list(),
73 threshold = NULL,
74 filters = list(),
75 exception_handler = default_exception_handler,
76 propagate = TRUE
77 ){
78 structure(
79 list(
80 appenders = standardize_appenders_list(appenders),
81 threshold = threshold,
82 filters = standardize_filters_list(filters),
83 exception_handler = exception_handler,
84 propagate = propagate
85 ),
86 class = c("parsed_logger_config", "list")
87 )
88 }
89
90
91
92
93 is_parsed_logger_config <- function(x){
94 inherits(x, "parsed_logger_config")
95 }
96
97
98
99
100 parse_logger_config <- function(
101 x,
102 defaults = parsed_logger_config()
103 ){
104 if (is_parsed_logger_config(x)){
105 return(x)
106 } else {
107 assert(all(names(x) %in% names(defaults)))
108 }
109
110
111 if (is_logger_config(x)){
112 objects <- resolve_r6_ctors(x)
113
114 res <- defaults
115
116 if ("appenders" %in% names(x))
117 res$appenders <- standardize_appenders_list(objects$appenders)
118
119 if ("exception_handler" %in% names(x))
120 res$exception_handler <- eval(parse(text = x[["exception_handler"]]))
121
122 if ("propagate" %in% names(x))
123 res$propagate <- as.logical(toupper(x[["propagate"]]))
124
125 if ("threshold" %in% names(x))
126 res$threshold <- standardize_threshold(x$threshold)
127
128 if ("filters" %in% names(x))
129 res$filters <- standardize_filters_list(objects$filters)
130
131 class(res) <- c("parsed_logger_config", "list")
132
133 } else {
134 res <- do.call(parsed_logger_config, x)
135
136 }
137
138 res
139 }
140
141
142
143
144 #' `as_logger_config()` coerces any supported \R object to a `logger_config`
145 #' object. You usually do not have to call this function directly, as it is
146 #' automatically invoked by the `config()` method of Loggers (see examples)
147 #'
148 #' @param x any \R object. Especially:
149 #' * A `character` scalar. This can either be the path to a
150 #' YAML file or a character scalar containing valid YAML
151 #' * a list containing the elements `appenders`, `threshold`, `exception_handler`,
152 #' `propagate` and `filters`. See the section *Fields* in [Logger] for
153 #' details.
154 #' * a Logger object, to clone its configuration.
155 #'
156 #' @rdname logger_config
157 #' @seealso \url{https://yaml.org/}
158 #' @return a logger_config object
159 #' @export
160 #'
161 as_logger_config <- function(x){
162 UseMethod("as_logger_config")
163 }
164
165
166
167
168 #' @export
169 as_logger_config.NULL <- function(x){
170 as_logger_config(list())
171 }
172
173
174
175
176 #' @rdname logger_config
177 #' @export
178 as_logger_config.list <- function(x){
179 if (identical(names(x), "Logger"))
180 x <- x[["Logger"]]
181
182 assert(all(
183 names(x) %in% c("exception_handler", "propagate", "threshold", "appenders", "filters")
184 ))
185 class(x) <- c("logger_config", class(x))
186 x
187 }
188
189
190
191
192 #' @rdname logger_config
193 #' @export
194 as_logger_config.character <- function(
195 x
196 ){
197 if (identical(length(x), 1L) && !grepl("\n", x)){
198 if (
199 identical(tolower(tools::file_ext(x)), "json") &&
200 requireNamespace("jsonlite", quietly = TRUE)
201 ){
202 # if jsonliste is installed use jsonlite for yaml, otherwiese use the
203 # yaml package (since JSON is also valid YAML)
204 dd <- jsonlite::read_json(x, simplifyVector = TRUE)
205
206 } else {
207 assert(file.exists(x), "The file '", x, "' does not exist.")
208 assert_namespace("yaml")
209 dd <- yaml::read_yaml(file = x)
210 }
211 } else {
212 dd <- yaml::read_yaml(text = x)
213 }
214
215 assert(
216 identical(names(dd), "Logger"),
217 "If 'x' is a YAML file, it must contain a single logger object"
218 )
219
220 as_logger_config(dd)
221 }
222
223
224
225
226 resolve_r6_ctors <- function(x){
227 ctors <- lapply(names(x), get0_R6Class)
228
229 for (i in seq_along(x)){
230 if (length(ctors) && !is.null(ctors[[i]])){
231
232 args <- resolve_r6_ctors(x[[i]])
233 if (is.null(args)) args <- list()
234
235 # prevent logger not named warning
236 suppressWarnings(x[[i]] <- do.call(ctors[[i]]$new, args))
237 } else {
238 if (is.recursive(x[[i]])){
239 x[[i]] <- resolve_r6_ctors(x[[i]])
240 } else {
241 x[[i]] <- x[[i]]
242 }
243 }
244 }
245
246 x
247 }
248
249
250
251
252 get0_R6Class <- function(x){
253 assert(is_scalar_character(x))
254 res <- get0(x, envir = parent.env(environment()))
255
256 if (R6::is.R6Class(res)){
257 res
258 } else {
259 NULL
260 }
261 }
262
263
264
265
266 standardize_appenders_list <- function(x){
267 if (is.null(x))
268 return(list())
269
270 if (inherits(x, "Appender"))
271 x <- list(x)
272
273 assert(
274 is.list(x) && all(vapply(x, inherits, TRUE, "Appender")),
275 "'appenders' must either be a single Appender, a list thereof, or ",
276 "NULL for no appenders."
277 )
278
279 x
280 }