| 1 |
#' A Fully Featured Logging Framework for R |
| 2 |
#' |
| 3 |
#' For details please refer to `vignette("lgr", package = "lgr")`. |
| 4 |
#' |
| 5 |
#' @section Options: |
| 6 |
#' |
| 7 |
#' You can also set these options in your `.Rprofile` to make them permanent. |
| 8 |
#' Some options can also be set via environment variables (The environment |
| 9 |
#' variables are only used if the option is not set manually from R). |
| 10 |
#' |
| 11 |
#' \describe{ |
| 12 |
#' \item{`lgr.colors`}{a `list` of `functions` used for coloring the log |
| 13 |
#' levels in console output. Usually these will be functions from the |
| 14 |
#' package **crayon**} |
| 15 |
#' \item{`lgr.log_levels`}{A named `integer` vector of log levels that are |
| 16 |
#' known to lgr for labeling, setting thresholds, etc... . Instead of |
| 17 |
#' modifying this option manually use [add_log_levels()] and |
| 18 |
#' [remove_log_levels()]} |
| 19 |
#' \item{`lgr.default_threshold`}{ |
| 20 |
#' `character` or `integer` scalar. The minimum [log level][log_levels] that |
| 21 |
#' should be processed by the root logger. Defaults to `400` (`"info"`), |
| 22 |
#' or to the value of the environment variable `LGR_DEFAULT_THRESHOLD` |
| 23 |
#' if it is set. This option overrides the threshold specified in |
| 24 |
#' `lgr.default_config` if both are set. |
| 25 |
#' } |
| 26 |
#' \item{`lgr.default_config`}{ |
| 27 |
#' Default configuration for the root logger. Can either be a special list |
| 28 |
#' object, a path to a YAML file, or a character scalar containing YAML |
| 29 |
#' code. See [logger_config] for details. Defaults to the value of the |
| 30 |
#' environment variable `LGR_DEFAULT_CONFIG` if it is set. |
| 31 |
#' } |
| 32 |
#' \item{`lgr.suspend_logging`}{`TRUE` or `FALSE`. Suspend all logging for |
| 33 |
#' all loggers. Defaults to the `TRUE` if the environment variable |
| 34 |
#' `LGR_SUSPEND_LOGGING` is set to `"TRUE"`. Instead of modifying this |
| 35 |
#' option manually use [suspend_logging()] and [unsuspend_logging()]} |
| 36 |
#' \item{`lgr.user`}{a `character` scalar. The default username for |
| 37 |
#' `lgr::get_user()`. |
| 38 |
#' } |
| 39 |
#' } |
| 40 |
#' |
| 41 |
#' @keywords internal |
| 42 |
#' @importFrom stats setNames |
| 43 |
"_PACKAGE" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#' @export lgr |
| 49 |
NULL |
| 50 |
|
| 51 |
.onLoad <- function(...){ |
| 52 |
|
| 53 |
# dyn s3 methods ------------------------------------------------------ |
| 54 |
dyn_register_s3_method("data.table", "as.data.table", "LogEvent") |
| 55 |
dyn_register_s3_method("data.table", "as.data.table", "event_list") |
| 56 |
dyn_register_s3_method("tibble", "as_tibble", "LogEvent") |
| 57 |
|
| 58 |
|
| 59 |
# options ------------------------------------------------------------- |
| 60 |
op <- options() |
| 61 |
op.this <- list() |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
# +- colors -------------------------------------------------------------- |
| 66 |
if (requireNamespace("crayon", quietly = TRUE) && crayon::has_color()){ |
| 67 |
style_error <- crayon::make_style("#BB3333", colors = 256) |
| 68 |
style_fatal <- function(...) style_error(crayon::bold(...)) |
| 69 |
style_warning <- crayon::make_style("#EEBB50", colors = 256) |
| 70 |
style_subtle <- crayon::make_style(grDevices::grey(0.5), grey = TRUE) |
| 71 |
style_accent <- crayon::make_style("#ca2c92", colors = 256) |
| 72 |
col_nchar <- crayon::col_nchar |
| 73 |
|
| 74 |
op.this[["lgr.colors"]] <- list( |
| 75 |
"fatal" = style_fatal, |
| 76 |
"error" = style_error, |
| 77 |
"warn" = style_warning, |
| 78 |
"debug" = style_subtle, |
| 79 |
"trace" = style_subtle |
| 80 |
) |
| 81 |
} else { |
| 82 |
style_fatal <- function(...) paste(...) |
| 83 |
style_error <- style_fatal |
| 84 |
style_warning <- style_fatal |
| 85 |
style_subtle <- style_fatal |
| 86 |
style_accent <- style_fatal |
| 87 |
col_nchar <- function(...) nchar(...) |
| 88 |
|
| 89 |
op.this[["lgr.colors"]] = list() |
| 90 |
} |
| 91 |
|
| 92 |
# make color functions available inside the package |
| 93 |
assign("style_fatal", style_fatal, envir = parent.env(environment())) |
| 94 |
assign("style_error", style_error, envir = parent.env(environment())) |
| 95 |
assign("style_warning", style_warning, envir = parent.env(environment())) |
| 96 |
assign("style_subtle", style_subtle, envir = parent.env(environment())) |
| 97 |
assign("style_accent", style_accent, envir = parent.env(environment())) |
| 98 |
assign("col_nchar", col_nchar, envir = parent.env(environment())) |
| 99 |
|
| 100 |
|
| 101 |
# +- log_levels -------------------------------------------------------------- |
| 102 |
op.this[["lgr.log_levels"]] <- c( |
| 103 |
"fatal" = 100L, |
| 104 |
"error" = 200L, |
| 105 |
"warn" = 300L, |
| 106 |
"info" = 400L, |
| 107 |
"debug" = 500L, |
| 108 |
"trace" = 600L |
| 109 |
) |
| 110 |
|
| 111 |
|
| 112 |
# +- options from envars ------------------------------------------------- |
| 113 |
op.this[["lgr.suspend_logging"]] <- get_envar_suspend_logging() |
| 114 |
op.this[["lgr.default_config"]] <- get_envar_default_config() |
| 115 |
op.this[["lgr.default_threshold"]] <- get_envar_default_threshold(fallback = 400) |
| 116 |
|
| 117 |
|
| 118 |
# +- set options (if they were not set manually before) ------------------ |
| 119 |
toset <- !(names(op.this) %in% names(op)) |
| 120 |
if(any(toset)) options(op.this[toset]) |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
# root looger ------------------------------------------------------------- |
| 125 |
assign( |
| 126 |
"root", |
| 127 |
LoggerRoot$new("root", threshold = NA), # threshold cannot be null |
| 128 |
envir = loggers |
| 129 |
) |
| 130 |
assign( |
| 131 |
"lgr", |
| 132 |
get_logger("root"), |
| 133 |
envir = parent.env(environment()) |
| 134 |
) |
| 135 |
|
| 136 |
# config |
| 137 |
default_config <- getOption("lgr.default_config") |
| 138 |
|
| 139 |
if (is.null(default_config)){ |
| 140 |
basic_config() |
| 141 |
|
| 142 |
} else { |
| 143 |
tryCatch( |
| 144 |
lgr$config(default_config), |
| 145 |
error = function(e) { |
| 146 |
warning( |
| 147 |
"The option 'lgr.default_config' is set to an invalid logger_config: ", |
| 148 |
e$message, call. = FALSE |
| 149 |
) |
| 150 |
} |
| 151 |
) |
| 152 |
} |
| 153 |
|
| 154 |
lgr$set_threshold(getOption("lgr.default_threshold", 400L)) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
# get environment variables ----------------------------------------------- |
| 161 |
|
| 162 |
get_envar_suspend_logging <- function(){ |
| 163 |
envar_suspend_logging <- toupper(Sys.getenv("LGR_SUSPEND_LOGGING")) |
| 164 |
|
| 165 |
if (identical(envar_suspend_logging, "TRUE")){ |
| 166 |
TRUE |
| 167 |
} else if ( |
| 168 |
identical(envar_suspend_logging, "FALSE") || |
| 169 |
is_blank(envar_suspend_logging) |
| 170 |
){ |
| 171 |
FALSE |
| 172 |
} else { |
| 173 |
warning( |
| 174 |
"Environment variable 'LGR_SUSPEND_LOGGING' is set to '", envar_suspend_logging, |
| 175 |
"' but must be either 'TRUE' or 'FALSE'", call. = FALSE) |
| 176 |
FALSE |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
get_envar_default_config <- function(){ |
| 184 |
envar_default_config <- Sys.getenv("LGR_DEFAULT_CONFIG") |
| 185 |
|
| 186 |
if (is_blank(envar_default_config)){ |
| 187 |
NULL |
| 188 |
} else { |
| 189 |
if (!is_scalar_character(envar_default_config)){ |
| 190 |
warning("Environment variable 'LGR_DEFAULT_CONFIG' must be a path to a YAML or JSON config file", call. = FALSE) |
| 191 |
NULL |
| 192 |
} else { |
| 193 |
tryCatch( |
| 194 |
as_logger_config(envar_default_config), |
| 195 |
error = function(e){ |
| 196 |
warning( |
| 197 |
"Environment variable 'LGR_DEFAULT_CONFIG' is set but '", envar_default_config, |
| 198 |
"' is not a path to a valid YAML file", call. = FALSE |
| 199 |
) |
| 200 |
NULL |
| 201 |
} |
| 202 |
) |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
get_envar_default_threshold <- function(fallback = 400){ |
| 211 |
envvar_default_threshold <- tolower(Sys.getenv("LGR_DEFAULT_THRESHOLD")) |
| 212 |
|
| 213 |
if (!is_scalar(envvar_default_threshold)){ |
| 214 |
warning( |
| 215 |
"Environment variable 'LGR_DEFAULT_THRESHOLD' bust be a single", |
| 216 |
"numeric or character value.", call. = FALSE |
| 217 |
) |
| 218 |
return(fallback) |
| 219 |
} |
| 220 |
|
| 221 |
int_threshold <- suppressWarnings(as.integer(envvar_default_threshold)) |
| 222 |
|
| 223 |
if (!is.na(int_threshold)) |
| 224 |
envvar_default_threshold <- int_threshold |
| 225 |
|
| 226 |
if (is_blank(envvar_default_threshold)){ |
| 227 |
fallback |
| 228 |
} else { |
| 229 |
tryCatch( |
| 230 |
standardize_threshold(envvar_default_threshold), |
| 231 |
error = function(e){ |
| 232 |
warning( |
| 233 |
"Environment variable 'LGR_DEFAULT_THRESHOLD' is set but '", envvar_default_threshold, |
| 234 |
"' is not a valid threshold", call. = FALSE |
| 235 |
) |
| 236 |
fallback |
| 237 |
} |
| 238 |
) |
| 239 |
} |
| 240 |
} |