Skip to content

Commit 8d764c2

Browse files
committed
doc / version bump
1 parent 3732649 commit 8d764c2

File tree

12 files changed

+174
-151
lines changed

12 files changed

+174
-151
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: lgr
33
Title: A Fully Featured Logging Framework
4-
Version: 0.2.0.9001
4+
Version: 0.9.0.9000
55
Authors@R:
66
person(given = "Stefan",
77
family = "Fleck",
@@ -41,7 +41,8 @@ Suggests:
4141
tibble,
4242
tools,
4343
utils,
44-
whoami
44+
whoami,
45+
yaml
4546
VignetteBuilder:
4647
knitr
4748
Encoding: UTF-8

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
S3method(as.data.frame,LogEvent)
4+
S3method(as_logger_config,Logger)
45
S3method(as_logger_config,character)
56
S3method(as_logger_config,list)
67
S3method(format,LogEvent)

R/Logger.R

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@
133133
#' filters.
134134
#' }
135135
#'
136+
#' \item{`config(cfg = NULL, file = NULL, text = NULL`}{Load a Logger
137+
#' configuration. `cfg` can be either a special list object, the path to
138+
#' a YAML config file, or a character scalar containing YAML. The arguments
139+
#' `file` and `text` can used alternatively and enforce that the supplied
140+
#' argument is of the specified type.
141+
#' See [logger_config] for details.
142+
#' }
143+
#'
136144
#' \item{`add_appender(appender, name = NULL)`, `remove_appender(pos)`}{
137145
#' Add or remove an [Appender]. Supplying a `name` is optional but
138146
#' recommended. After adding an Appender with
@@ -247,46 +255,6 @@ Logger <- R6::R6Class(
247255
invisible(self)
248256
},
249257

250-
config = function(
251-
cfg = NULL,
252-
file = NULL,
253-
text = NULL
254-
){
255-
assert(
256-
is.null(cfg) + is.null(file) + is.null(text) >= 2,
257-
"You can only specify one of `cfg`, `file` and `text`."
258-
)
259-
260-
if (!is.null(cfg)){
261-
cfg <- as_logger_config(cfg)
262-
263-
} else if (!is.null(file)){
264-
assert(
265-
is_scalar_character(file) && !grepl("\n", file) && file.exists(file),
266-
"`file` is not a valid path to a readable file"
267-
)
268-
cfg <- as_logger_config(file)
269-
270-
} else if (!is.null(text)){
271-
assert(
272-
is_scalar_character(text) && grepl("\n", text),
273-
"`text` must be a character scalar containing valid YAML"
274-
)
275-
cfg <- as_logger_config(text)
276-
277-
} else {
278-
cfg <- as_logger_config()
279-
}
280-
281-
self$set_threshold(cfg$threshold)
282-
self$set_appenders(cfg$appenders)
283-
self$set_propagate(cfg$propagate)
284-
self$set_filters(cfg$filters)
285-
self$set_exception_handler(cfg$exception_handler)
286-
287-
self
288-
},
289-
290258

291259
log = function(
292260
level,
@@ -453,6 +421,48 @@ Logger <- R6::R6Class(
453421
)
454422
},
455423

424+
425+
config = function(
426+
cfg = NULL,
427+
file = NULL,
428+
text = NULL
429+
){
430+
assert(
431+
is.null(cfg) + is.null(file) + is.null(text) >= 2,
432+
"You can only specify one of `cfg`, `file` and `text`."
433+
)
434+
435+
if (!is.null(cfg)){
436+
cfg <- as_logger_config(cfg)
437+
438+
} else if (!is.null(file)){
439+
assert(
440+
is_scalar_character(file) && !grepl("\n", file) && file.exists(file),
441+
"`file` is not a valid path to a readable file"
442+
)
443+
cfg <- as_logger_config(file)
444+
445+
} else if (!is.null(text)){
446+
assert(
447+
is_scalar_character(text) && grepl("\n", text),
448+
"`text` must be a character scalar containing valid YAML"
449+
)
450+
cfg <- as_logger_config(text)
451+
452+
} else {
453+
cfg <- as_logger_config()
454+
}
455+
456+
self$set_threshold(cfg$threshold)
457+
self$set_appenders(cfg$appenders)
458+
self$set_propagate(cfg$propagate)
459+
self$set_filters(cfg$filters)
460+
self$set_exception_handler(cfg$exception_handler)
461+
462+
self
463+
},
464+
465+
456466
add_appender = function(
457467
appender,
458468
name = NULL

R/logger_config.R

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
#' Logger Configuration Objects
22
#'
3-
#' `logger_config()` and `basic_config()` can be used to directly create
4-
#' objects that can be passed to the `$config()` method of Loggers.
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.
57
#'
6-
#' @param appenders,threshold,filters,exception_handler,propagate see [Logger]
78
#'
8-
#' @return a `list` with subclass `"logger_config"`
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"`
916
#' @export
1017
#'
1118
#' @examples
19+
#' lg <- get_logger("test")
20+
#'
21+
#' # explicetely defining logger configurations with logger_config
22+
#'
1223
#' # call without arguments to generate the default configuration
13-
#' cfg <- logger_config()
24+
#' cfg <- logger_config() # same as the unconfigured state of a Logger
25+
#' lg$config(cfg)
26+
#'
1427
#'
28+
#' # Creating a logger config form YAML
29+
#' cfg <- "
30+
#' Logger:
31+
#' name: test/blubb
32+
#' threshold: info
33+
#' propagate: false
34+
#' appenders:
35+
#' AppenderFile:
36+
#' file: /tmp/blah.txt
37+
#' "
38+
#' lg$config(cfg) # calls as_logger_config() internally
1539
logger_config <- function(
1640
appenders = list(),
1741
threshold = NULL,
@@ -46,39 +70,25 @@ logger_config <- function(
4670
#'
4771
#' @param x any \R object. Especially:
4872
#' * A `character` scalar. This can either be the path to a
49-
#' [YAML][https://yaml.org/] file, or directly valid YAML
73+
#' YAML file or a character scalar containing valid YAML
5074
#' * a list containing the elements `appenders`, `threshold`, `exception_handler`,
5175
#' `propagate` and `filters`. See the section *Fields* in [Logger] for
5276
#' details.
5377
#' * a Logger object, to clone its configuration.
5478
#'
5579
#' @rdname logger_config
80+
#' @seealso \url{https://www.yaml.org/}
5681
#' @return a logger_config object
5782
#' @export
5883
#'
59-
#' @examples
60-
#' cfg <- "
61-
#' Logger:
62-
#' name: test/blubb
63-
#' threshold: info
64-
#' propagate: false
65-
#' appenders:
66-
#' AppenderFile:
67-
#' file: /tmp/blah.txt
68-
#' "
69-
#' lg$config(as_logger_config(cfg))
70-
#' # but you can just do the following directly
71-
#' lg <- get_logger("test")
72-
#' lg$config(cfg)
73-
#'
7484
as_logger_config <- function(x){
7585
UseMethod("as_logger_config")
7686
}
7787

7888

7989

8090

81-
#' @rdname as_logger_config
91+
#' @rdname logger_config
8292
#' @export
8393
as_logger_config.list <- function(x){
8494
assert(is.list(x))
@@ -99,7 +109,7 @@ as_logger_config.list <- function(x){
99109

100110

101111

102-
#' @rdname as_logger_config
112+
#' @rdname logger_config
103113
#' @export
104114
as_logger_config.character <- function(
105115
x
@@ -125,8 +135,8 @@ as_logger_config.character <- function(
125135

126136

127137

128-
#' @param x
129-
#' @rdname as_logger_config
138+
#' @export
139+
#' @rdname logger_config
130140
as_logger_config.Logger <- function(x){
131141
logger_config(
132142
appenders = x$appenders,
@@ -232,27 +242,3 @@ standardize_filters_list <- function(x){
232242

233243
x
234244
}
235-
236-
237-
238-
#' Title
239-
#' @rdname logger_config
240-
#' @param file `character` scalar.
241-
#' @param fmt
242-
#' @param fmt_timestamp
243-
#' @param threshold
244-
#' @param appenders
245-
#'
246-
#' @return
247-
#' @export
248-
#'
249-
#' @examples
250-
basic_config = function(
251-
file,
252-
fmt,
253-
fmt_timestamp,
254-
threshold,
255-
appenders
256-
){
257-
258-
}

R/simple_logging.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#' These functions provide a simple interface to the root logger. If you do not
44
#' need any of the more advanced features of lgr, start here.
55
#'
6-
#'
76
#' @name simple_logging
87
#'
98
#' @examples
@@ -19,7 +18,7 @@ NULL
1918

2019
#' Basic Setup for the Logging Systen
2120
#'
22-
#' Quick and easy way to configure the root logger for logging to a root file
21+
#' Quick and easy way to configure the root logger for logging to a file
2322
#'
2423
#' @param file `character` scalar: If not `NULL` a [AppenderFile] will be created
2524
#' that logs to this file. If the filename ends in `.jsonl` an [AppenderJson]
@@ -29,16 +28,20 @@ NULL
2928
#' (see [format.LogEvent])
3029
#' @inheritParams print.LogEvent
3130
#' @inheritParams Logger
32-
#' @param appenders
31+
#' @param appenders a single [Appender] or a list thereof. Musst be `NULL` if
32+
#' if `file` is already specified.
33+
#' @param threshold `character` or `integer` scalar.
34+
#' The minimum [log level][log_levels] that should be processed by the root
35+
#' logger.
3336
#'
3437
#' @return `NULL` (invisibly)
3538
#' @export
3639
#'
3740
#' @examples
38-
#'
39-
#' basic_config(tempfile())
40-
#' print(lgr)
41-
#'
41+
#' \dontrun{
42+
#' make the root logger log to a file
43+
#' basic_config(file = tempfile())
44+
#' }
4245
basic_config <- function(
4346
file = NULL,
4447
fmt = NULL,
@@ -50,7 +53,7 @@ basic_config <- function(
5053

5154

5255
if (!is.null(file)){
53-
assert(is.null(appenders), "`appenders` must be null if `file` is specified")
56+
assert(is.null(appenders), "`appenders` must be NULL if `file` is specified")
5457

5558
pos <- regexpr("\\.([[:alnum:]]+)$", file)
5659
ext <- ifelse(pos > -1L, substring(file, pos + 1L), "")
@@ -76,9 +79,6 @@ basic_config <- function(
7679
l$set_appenders(appenders)
7780
l$set_threshold(threshold)
7881

79-
# reset to defaults
80-
l$set_exception_handler(default_exception_handler)
81-
8282
invisible(NULL)
8383
}
8484

R/utils-logging.R

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
#'
1515
#' # temporarily disable logging
1616
#' lg$fatal("foo")
17-
#' without_logging(lg$fatal("bar"))
17+
#' without_logging({
18+
#' lg$info("everything in this codeblock will be suppressed")
19+
#' lg$fatal("bar")
20+
#' })
1821
#'
1922
#' # globally disable logging
2023
#' suspend_logging()
@@ -45,8 +48,6 @@ unsuspend_logging <- function(){
4548
#' @rdname suspend_logging
4649
#' @param code Any \R code
4750
#' @export
48-
#' @examples
49-
#'
5051
without_logging <- function(code){
5152
old <- getOption("lgr.logging_suspended")
5253
on.exit(options(lgr.logging_suspended = old))
@@ -58,13 +59,8 @@ without_logging <- function(code){
5859

5960

6061

61-
#'
62-
#'
6362
#' @rdname suspend_logging
64-
#' @param code Any \R code
6563
#' @export
66-
#' @examples
67-
#'
6864
with_logging <- function(code){
6965
old <- getOption("lgr.logging_suspended")
7066
on.exit(options(lgr.logging_suspended = old))

README.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ lgr is currently very actively developed, and feature requests are encouraged.
150150

151151

152152

153-
154153
## Dependencies
155154

156155
[R6](https://github.com/r-lib/R6): The R6 class system prevents the framework
@@ -194,6 +193,8 @@ dependencies, and are well maintained :
194193
manually if you want to use it for logging.
195194
- [desc](https://CRAN.R-project.org/package=desc) for the package development
196195
convenince function `use_logger()`
196+
- [yaml](https://CRAN.R-project.org/package=yaml) for configuring loggers
197+
via YAML files
197198

198199
Other optional dependencies (future, future.apply) do not provide any extra
199200
functionallity but had to be included for some of the automated unit tests

0 commit comments

Comments
 (0)