Skip to content

Commit b5e8529

Browse files
committed
as_logger_config.list now uses logger_config
1 parent 2a06f65 commit b5e8529

File tree

8 files changed

+72
-37
lines changed

8 files changed

+72
-37
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export(is_filter)
6363
export(label_levels)
6464
export(lgr)
6565
export(log_exception)
66+
export(logger_config)
6667
export(pad_left)
6768
export(pad_right)
6869
export(read_json_lines)

R/Logger.R

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,22 @@ Logger <- R6::R6Class(
235235
)
236236
}
237237

238-
self$set_appenders(appenders)
239-
self$set_exception_handler(exception_handler)
240238
self$set_name(name)
241-
self$set_propagate(propagate)
242-
self$set_filters(filters)
243239
private$.last_event <- LogEvent$new(self)
240+
244241
self$set_threshold(threshold)
242+
self$set_appenders(appenders)
243+
self$set_propagate(propagate)
244+
self$set_filters(filters)
245+
self$set_exception_handler(exception_handler)
245246

246247
invisible(self)
247248
},
248249

249-
250250
config = function(
251251
cfg = NULL,
252252
file = NULL,
253-
text = NULL,
254-
...,
255-
appenders,
256-
threshold,
257-
filters,
258-
exception_handler,
259-
propagate
253+
text = NULL
260254
){
261255
assert(
262256
is.null(cfg) + is.null(file) + is.null(text) >= 2,
@@ -276,7 +270,7 @@ Logger <- R6::R6Class(
276270
} else if (!is.null(text)){
277271
assert(
278272
is_scalar_character(text) && grepl("\n", text),
279-
"`text` must be a character scalar containing valid yaml"
273+
"`text` must be a character scalar containing valid YAML"
280274
)
281275
cfg <- as_logger_config(text)
282276

R/log_levels.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ colorize_levels <- function(
198198
#' @noRd
199199
standardize_threshold <- function(
200200
x,
201-
log_levels = c(getOption("lgr.log_levels"), c("all" = NA_integer_, "off" = 0L))
201+
log_levels = c(getOption("lgr.log_levels"), c("all" = NA_integer_, "off" = 0L)),
202+
allow_null = FALSE
202203
){
203204
assert(is_scalar(x), "A threshold must be a scalar (a vector of length 1)" )
204205

R/logger_config.R

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#'
77
#' @param x any \R object. Especially:
88
#' * A `character` scalar. This can either be the path to a
9-
#' [yaml][https://yaml.org/] file, or directly valid yaml.
9+
#' [YAML][https://yaml.org/] file, or directly valid YAML
1010
#' * a list containing the elements `appenders`, `threshold`, `exception_handler`,
1111
#' `propagate` and `filters`. See the section *Fields* in [Logger] for
1212
#' details.
@@ -38,6 +38,21 @@ as_logger_config <- function(x){
3838

3939

4040

41+
#' Create a Logger configuration object
42+
#'
43+
#' @param appenders
44+
#' @param threshold
45+
#' @param filters
46+
#' @param exception_handler
47+
#' @param propagate
48+
#'
49+
#' @return a `list` with subclass `"logger_config"`
50+
#' @export
51+
#'
52+
#' @examples
53+
#' # call without arguments to generate the default configuration
54+
#' cfg <- logger_config()
55+
#'
4156
logger_config <- function(
4257
appenders = list(),
4358
threshold = NULL,
@@ -48,10 +63,14 @@ logger_config <- function(
4863
assert(is.function(exception_handler))
4964
assert(is_scalar_bool(propagate))
5065

66+
if (!is.null(threshold)){
67+
threshold <- standardize_threshold(threshold)
68+
}
69+
5170
structure(
5271
list(
5372
appenders = standardize_appenders_list(appenders),
54-
threshold = standardize_threshold(threshold),
73+
threshold = threshold,
5574
filters = standardize_filters_list(filters),
5675
exception_handler = exception_handler,
5776
propagate = propagate
@@ -69,19 +88,16 @@ logger_config <- function(
6988
as_logger_config.list <- function(x){
7089
assert(is.list(x))
7190

72-
assert(all(
73-
names(x) %in% c("exception_handler", "propagate", "threshold", "appenders", "filters")
91+
assert(setequal(
92+
names(x), c("exception_handler", "propagate", "threshold", "appenders", "filters")
7493
))
7594

76-
assert(is.function(x$exception_handler))
77-
assert(is_scalar_bool(x$propagate))
78-
79-
x$threshold <- standardize_threshold(x$threshold)
80-
x$filters <- standardize_filters_list(x$filters)
81-
x$appenders <- standardize_appenders_list(x$appenders)
82-
83-
class(x) <- c("logger_config", "list")
84-
x
95+
logger_config(
96+
threshold = x$threshold,
97+
filters = x$filters,
98+
appenders = x$appenders,
99+
exception_handler = x$exception_handler
100+
)
85101
}
86102

87103

@@ -102,14 +118,14 @@ as_logger_config.character <- function(
102118

103119
assert(
104120
identical(length(names(dd)), 1L),
105-
"If 'x' is a yaml file, it must contain a single logger object"
121+
"If 'x' is a YAML file, it must contain a single logger object"
106122
)
107123

108124
res <- resolve_r6_ctors(dd)
109125

110126
assert(
111127
is_Logger(res),
112-
"If `x` is a yaml file or string, it must contain a single logger object"
128+
"If `x` is a YAML file or string, it must contain a single logger object"
113129
)
114130

115131
res

man/as_logger_config.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/logger_config.Rd

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

tests/testthat/test_parallel.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ for (strategy in c(
2121
tf <- tempfile()
2222

2323
lr <- get_logger("par_root")
24-
lr$config(
24+
lr$config(logger_config(
2525
propagate = FALSE,
2626
appenders = AppenderFile$new(tf)
27-
)
27+
))
2828

2929
lg <- get_logger("par_root/par_child")
3030

vignettes/lgr.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,14 @@ just set the buffer size to `0`.
691691
```{r}
692692
# install.packages("RSQLite")
693693
lg <- get_logger("db_logger")
694-
695-
lg$config(
696-
appenders = list(db = AppenderDbi$new(
694+
lg$set_propagate(FALSE)
695+
lg$add_appender(
696+
name = "db",
697+
AppenderDbi$new(
697698
conn = DBI::dbConnect(RSQLite::SQLite()),
698699
table = "log",
699700
buffer_size = 2L
700-
)),
701-
propagate = FALSE
701+
)
702702
)
703703
704704
lg$info("Logging to databases uses a buffer")

0 commit comments

Comments
 (0)