| 1 |
#' Basic Setup for the Logging System |
| 2 |
#' |
| 3 |
#' A quick and easy way to configure the root logger. This is less powerful |
| 4 |
#' then using `lgr$config()` or `lgr$set_*()` (see [Logger]), but reduces the |
| 5 |
#' most common configurations to a single line of code. |
| 6 |
#' |
| 7 |
#' @param file `character` scalar: If not `NULL` a [AppenderFile] will be |
| 8 |
#' created that logs to this file. If the filename ends in `.jsonl`, the |
| 9 |
#' Appender will be set up to use the [JSON Lines](https://jsonlines.org/) |
| 10 |
#' format instead of plain text (see [AppenderFile] and [AppenderJson]). |
| 11 |
#' @param fmt `character` scalar: Format to use if `file` is supplied and not a |
| 12 |
#' `.jsonl` file. If `NULL` it defaults to `"%L [%t] %m"` (see |
| 13 |
#' [format.LogEvent]) |
| 14 |
#' @param console_fmt `character` scalar: like `fmt` but used for console output |
| 15 |
#' @param console_timestamp_fmt `character` scalar: like `timestamp_fmt` but |
| 16 |
#' used for console output |
| 17 |
#' @param console_connection see [cat()] `file` argument. |
| 18 |
#' @inheritParams print.LogEvent |
| 19 |
#' @inheritParams Logger |
| 20 |
#' @param appenders a single [Appender] or a list thereof. |
| 21 |
#' @param threshold `character` or `integer` scalar. The minimum [log |
| 22 |
#' level][log_levels] that should be processed by the root logger. |
| 23 |
#' @param memory `logical` scalar. or a `threshold` (see above). Add an Appender |
| 24 |
#' that logs to a memory buffer, see also [show_log()] and [AppenderBuffer] |
| 25 |
#' @param console `logical` scalar or a `threshold` (see above). Add an appender |
| 26 |
#' logs to the console (i.e. displays messages in an interactive R session) |
| 27 |
#' |
| 28 |
#' @return the `root` Logger (lgr) |
| 29 |
#' @export |
| 30 |
#' |
| 31 |
#' @examples |
| 32 |
#' # log to a file |
| 33 |
#' basic_config(file = tempfile()) |
| 34 |
#' unlink(lgr$appenders$file$file) # cleanup |
| 35 |
#' |
| 36 |
# # log to a JSON file |
| 37 |
#' basic_config(file = tempfile(fileext = "jsonl")) |
| 38 |
#' unlink(lgr$appenders$file$file) # cleanup |
| 39 |
#' |
| 40 |
#' # log debug messages to a memory buffer |
| 41 |
#' basic_config(threshold = "all", memory = "all", console = "info") |
| 42 |
#' lgr$info("an info message") |
| 43 |
#' lgr$debug("a hidden message") |
| 44 |
#' show_log() |
| 45 |
#' |
| 46 |
#' # reset to default config |
| 47 |
#' basic_config() |
| 48 |
basic_config <- function( |
| 49 |
file = NULL, |
| 50 |
fmt = "%L [%t] %m", |
| 51 |
timestamp_fmt = "%Y-%m-%d %H:%M:%OS3", |
| 52 |
threshold = "info", |
| 53 |
appenders = NULL, |
| 54 |
console = if (is.null(appenders)) "all" else FALSE, |
| 55 |
console_fmt = "%L [%t] %m %f", |
| 56 |
console_timestamp_fmt = "%H:%M:%OS3", |
| 57 |
console_connection = NULL, |
| 58 |
memory = FALSE |
| 59 |
){ |
| 60 |
default_fmt = "%L [%t] %m" # only relevant for triggering warning when logging to json |
| 61 |
|
| 62 |
stopifnot( |
| 63 |
is.null(file) || is_scalar_character(file), |
| 64 |
is.null(fmt) || is_scalar_character(fmt), |
| 65 |
is_scalar_character(console_fmt), |
| 66 |
is_scalar_character(timestamp_fmt), |
| 67 |
is_threshold(threshold), |
| 68 |
is_scalar_bool(console) || is_threshold(console), |
| 69 |
is_scalar_bool(memory) || is_threshold(console), |
| 70 |
is.null(appenders) || is.list(appenders) || inherits(appenders, "Appender") |
| 71 |
) |
| 72 |
|
| 73 |
l <- |
| 74 |
get_logger()$ |
| 75 |
config(NULL)$ |
| 76 |
set_threshold(threshold) |
| 77 |
|
| 78 |
|
| 79 |
if (length(appenders)){ |
| 80 |
assert( |
| 81 |
is.null(file) || !"file" %in% names(appenders), |
| 82 |
"If `appenders` contains an appender named `file`, the `file` argument to basic_config() must be `NULL`" |
| 83 |
) |
| 84 |
assert( |
| 85 |
isFALSE(console) || !"console" %in% names(appenders), |
| 86 |
"If `appenders` contains an appender named `console`, the `console` argument to basic_config() must be `FALSE`" |
| 87 |
) |
| 88 |
assert( |
| 89 |
isFALSE(memory) || !"memory" %in% names(appenders), |
| 90 |
"If `appenders` contains an appender named `memory`, the `memory` argument to basic_config() must be `FALSE`" |
| 91 |
) |
| 92 |
|
| 93 |
l$set_appenders(appenders) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
if (!is.null(file)){ |
| 98 |
ext <- tools::file_ext(file) |
| 99 |
|
| 100 |
if (tolower(ext) %in% c("jsonl", "json")){ |
| 101 |
|
| 102 |
if (identical(tolower(ext), "json")){ |
| 103 |
warning( |
| 104 |
"Please use `.jsonl` and not `.json` as file extension for JSON log ", |
| 105 |
"files. The reason is that that JSON files created ", |
| 106 |
"by lgr are not true JSON files but JSONlines files. ", |
| 107 |
"See https://jsonlines.org/ for more infos." |
| 108 |
) |
| 109 |
} |
| 110 |
|
| 111 |
if (!is.null(fmt) && !identical(fmt, default_fmt)){ |
| 112 |
warning("`fmt` is ignored if `file` is a '.jsonl' file") |
| 113 |
} |
| 114 |
|
| 115 |
l$add_appender( |
| 116 |
name = "file", |
| 117 |
AppenderJson$new(file = file, threshold = NA) |
| 118 |
) |
| 119 |
|
| 120 |
} else { |
| 121 |
l$add_appender( |
| 122 |
name = "file", |
| 123 |
AppenderFile$new( |
| 124 |
file = file, |
| 125 |
threshold = NA, |
| 126 |
layout = LayoutFormat$new( |
| 127 |
fmt = fmt, |
| 128 |
timestamp_fmt = timestamp_fmt |
| 129 |
) |
| 130 |
) |
| 131 |
) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (!isFALSE(console)){ |
| 137 |
if (isTRUE(console)) console <- 400 |
| 138 |
l$add_appender( |
| 139 |
name = "console", |
| 140 |
AppenderConsole$new( |
| 141 |
threshold = console, |
| 142 |
connection = console_connection, |
| 143 |
layout = LayoutFormat$new( |
| 144 |
colors = getOption("lgr.colors"), |
| 145 |
fmt = console_fmt, |
| 146 |
timestamp_fmt = console_timestamp_fmt |
| 147 |
) |
| 148 |
) |
| 149 |
) |
| 150 |
} |
| 151 |
|
| 152 |
if (!isFALSE(memory)){ |
| 153 |
if (isTRUE(memory)) memory <- NA |
| 154 |
l$add_appender(name = "memory", AppenderBuffer$new( |
| 155 |
threshold = memory, |
| 156 |
should_flush = NULL |
| 157 |
)) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
lgr |
| 162 |
} |