| 1 |
# Layout ------------------------------------------------------------------ |
| 2 |
|
| 3 |
|
| 4 |
#' Abstract Class for Layouts |
| 5 |
#' |
| 6 |
#' [Appenders] pass [LogEvents][LogEvent] to a Layout which formats it for |
| 7 |
#' output. For the Layouts included in lgr that means turning the LogEvent |
| 8 |
#' into a `character` string. |
| 9 |
#' |
| 10 |
#' For each Appender exist one more more possible Layouts, but not every Layout |
| 11 |
#' will work with every Appender. See the package \pkg{lgrExtra} for examples |
| 12 |
#' for Layouts that return different data types (such as `data.frames`) and |
| 13 |
#' Appenders that can handle them. |
| 14 |
#' |
| 15 |
#' @section Notes for developers: |
| 16 |
#' Layouts may have an additional `$read(file, threshold, n)` method that returns |
| 17 |
#' a `character` vector, and/or an `$parse(file)` method that |
| 18 |
#' returns a `data.frame`. These can be used by Appenders to `$show()` methods |
| 19 |
#' and `$data` active bindings respectively (see source code of [AppenderFile]). |
| 20 |
#' |
| 21 |
#' @aliases Layouts |
| 22 |
#' @family Layouts |
| 23 |
#' @include LogEvent.R |
| 24 |
#' @include utils.R |
| 25 |
#' @include utils-sfmisc.R |
| 26 |
#' @include Filterable.R |
| 27 |
#' @include log_levels.R |
| 28 |
#' |
| 29 |
#' @export |
| 30 |
Layout <- R6::R6Class( |
| 31 |
"Layout", |
| 32 |
|
| 33 |
public = list( |
| 34 |
|
| 35 |
#' @description Format a log event |
| 36 |
#' |
| 37 |
#' Function that the Layout uses to transform a [LogEvent] into something |
| 38 |
#' that an [Appender] can write to an output destination. |
| 39 |
#' |
| 40 |
#' @param event a [LogEvent] |
| 41 |
format_event = function(event){ |
| 42 |
toString(event) |
| 43 |
}, |
| 44 |
|
| 45 |
toString = function() fmt_class(class(self)[[1]]), |
| 46 |
|
| 47 |
# . . setters ----------------------------------------------------------------- |
| 48 |
set_excluded_fields = function(x){ |
| 49 |
assert(is.null(x) || is.character(x)) |
| 50 |
private$.excluded_fields <- x |
| 51 |
invisible(self) |
| 52 |
} |
| 53 |
), |
| 54 |
|
| 55 |
|
| 56 |
# . . active -------------------------------------------------------------- |
| 57 |
active = list( |
| 58 |
#' @field excluded_fields fields to exclude from the final log |
| 59 |
excluded_fields = function() { |
| 60 |
get(".excluded_fields", private) |
| 61 |
} |
| 62 |
), |
| 63 |
|
| 64 |
|
| 65 |
# . . private ------------------------------------------------------------- |
| 66 |
private = list( |
| 67 |
.excluded_fields = NULL |
| 68 |
) |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
# LayoutFormat ------------------------------------------------------------ |
| 75 |
|
| 76 |
#' Format Log Events as Text |
| 77 |
#' |
| 78 |
#' Format a [LogEvent] as human readable text using [format.LogEvent()], which |
| 79 |
#' provides a quick and easy way to customize log messages. If you need |
| 80 |
#' more control and flexibility, consider using [LayoutGlue] instead. |
| 81 |
#' |
| 82 |
#' @inheritSection print.LogEvent Format Tokens |
| 83 |
#' |
| 84 |
#' @section Format Tokens: |
| 85 |
#' This is the same list of format tokens as for [format.LogEvent()] |
| 86 |
#' |
| 87 |
#' |
| 88 |
#' @export |
| 89 |
#' @family Layouts |
| 90 |
#' @include Filterable.R |
| 91 |
#' @include log_levels.R |
| 92 |
#' @examples |
| 93 |
#' # setup a dummy LogEvent |
| 94 |
#' event <- LogEvent$new( |
| 95 |
#' logger = Logger$new("dummy logger"), |
| 96 |
#' level = 200, |
| 97 |
#' timestamp = Sys.time(), |
| 98 |
#' caller = NA_character_, |
| 99 |
#' msg = "a test message" |
| 100 |
#' ) |
| 101 |
#' lo <- LayoutFormat$new() |
| 102 |
#' lo$format_event(event) |
| 103 |
LayoutFormat <- R6::R6Class( |
| 104 |
"LayoutFormat", |
| 105 |
inherit = Layout, |
| 106 |
public = list( |
| 107 |
initialize = function( |
| 108 |
fmt = "%L [%t] %m %j", |
| 109 |
timestamp_fmt = "%Y-%m-%d %H:%M:%OS3", |
| 110 |
colors = NULL, |
| 111 |
pad_levels = "right", |
| 112 |
excluded_fields = NULL |
| 113 |
){ |
| 114 |
self$set_fmt(fmt) |
| 115 |
self$set_timestamp_fmt(timestamp_fmt) |
| 116 |
self$set_colors(colors) |
| 117 |
self$set_pad_levels(pad_levels) |
| 118 |
self$set_excluded_fields(excluded_fields) |
| 119 |
}, |
| 120 |
|
| 121 |
#' @description Format a LogEvent |
| 122 |
#' @param event a [LogEvent] |
| 123 |
format_event = function( |
| 124 |
event |
| 125 |
){ |
| 126 |
format.LogEvent( |
| 127 |
event, |
| 128 |
fmt = private$.fmt, |
| 129 |
timestamp_fmt = private$.timestamp_fmt, |
| 130 |
colors = private$.colors, |
| 131 |
pad_levels = private$.pad_levels, |
| 132 |
excluded_fields = private$.excluded_fields |
| 133 |
) |
| 134 |
}, |
| 135 |
|
| 136 |
#' @details see Fields |
| 137 |
set_fmt = function(x){ |
| 138 |
assert(is_scalar_character(x)) |
| 139 |
private$.fmt <- x |
| 140 |
invisible(self) |
| 141 |
}, |
| 142 |
|
| 143 |
#' @details see Fields |
| 144 |
set_timestamp_fmt = function(x){ |
| 145 |
assert(is_scalar_character(x)) |
| 146 |
private$.timestamp_fmt <- x |
| 147 |
invisible(self) |
| 148 |
}, |
| 149 |
|
| 150 |
#' @details see Fields |
| 151 |
set_colors = function(x){ |
| 152 |
assert( |
| 153 |
is.null(x) || is.list(x), |
| 154 |
"'colors' must either be NULL or a list of functions, not ", |
| 155 |
class_fmt(x) |
| 156 |
) |
| 157 |
private$.colors <- x |
| 158 |
invisible(self) |
| 159 |
}, |
| 160 |
|
| 161 |
#' @details see Fields |
| 162 |
set_pad_levels = function(x){ |
| 163 |
assert(is_scalar_character(x)) |
| 164 |
private$.pad_levels <- x |
| 165 |
invisible(self) |
| 166 |
}, |
| 167 |
|
| 168 |
#' @details |
| 169 |
#' Convert Layout to a `character` string |
| 170 |
toString = function(){ |
| 171 |
paste(fmt_class(class(self)[[1]]), self$fmt) |
| 172 |
}, |
| 173 |
|
| 174 |
#' Read a log file written using LayoutFormat |
| 175 |
#' @param threshold a `character` or `integer` threshold |
| 176 |
#' @param n number of log entries to display |
| 177 |
read = function( |
| 178 |
file, |
| 179 |
threshold = NA_integer_, |
| 180 |
n = 20L |
| 181 |
){ |
| 182 |
assert(is_scalar_integerish(n)) |
| 183 |
threshold_ori <- threshold |
| 184 |
threshold <- standardize_threshold(threshold) |
| 185 |
|
| 186 |
dd <- readLines(file) |
| 187 |
sel <- TRUE |
| 188 |
|
| 189 |
if (!is.na(threshold)){ |
| 190 |
lvls_keep <- get_log_levels()[get_log_levels() <= threshold] |
| 191 |
|
| 192 |
if (grepl("%L", self$fmt, ignore.case = TRUE)){ |
| 193 |
sel <- grep( |
| 194 |
paste0("(", names(lvls_keep), ")", collapse = "|"), |
| 195 |
dd, |
| 196 |
ignore.case = TRUE |
| 197 |
) |
| 198 |
|
| 199 |
} else if (grepl("%n", self$fmt)){ |
| 200 |
sel <- grep(paste0("(", lvls_keep, ")", collapse = "|"), dd) |
| 201 |
|
| 202 |
} else { |
| 203 |
warning(sprintf(paste( |
| 204 |
"A threshold of `%s` was but the Layout's format specification", |
| 205 |
"('%s') does not support filtering by log level." |
| 206 |
)), threshold_ori, self$fmt ) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
dd <- tail(dd[sel], n) |
| 211 |
dd |
| 212 |
} |
| 213 |
), |
| 214 |
|
| 215 |
|
| 216 |
active = list( |
| 217 |
#' @field fmt a `character` scalar containing format tokens. See [format.LogEvent()]. |
| 218 |
fmt = function() private$.fmt, |
| 219 |
|
| 220 |
#' @field timestamp_fmt a `character` scalar. See [base::format.POSIXct()]. |
| 221 |
timestamp_fmt = function() private$.timestamp_fmt, |
| 222 |
|
| 223 |
#' @field colors a named `list` of functions (like the ones provided by |
| 224 |
#' the package \pkg{crayon}) passed on on [format.LogEvent()]. |
| 225 |
colors = function() private$.colors, |
| 226 |
|
| 227 |
#' @field pad_levels `"right"`, `"left"` or `NULL`. See [format.LogEvent()]. |
| 228 |
pad_levels = function() private$.pad_levels |
| 229 |
), |
| 230 |
|
| 231 |
private = list( |
| 232 |
.fmt = NULL, |
| 233 |
.timestamp_fmt = NULL, |
| 234 |
.colors = NULL, |
| 235 |
.pad_levels = NULL |
| 236 |
) |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
# LayoutGlue ------------------------------------------------------------ |
| 243 |
|
| 244 |
#' Format Log Events as Text via glue |
| 245 |
#' |
| 246 |
#' Format a [LogEvent] as human readable text using [glue::glue]. The function |
| 247 |
#' is evaluated in an environment in which it has access to all elements of |
| 248 |
#' the [LogEvent] (see examples). This is more flexible than [LayoutFormat], |
| 249 |
#' but also more complex and slightly less performant. |
| 250 |
#' |
| 251 |
#' @export |
| 252 |
#' @family Layouts |
| 253 |
#' @seealso lgr exports a number of formatting utility functions that are |
| 254 |
#' useful for layout glue: [colorize_levels()], [pad_left()], [pad_right()]. |
| 255 |
#' @examples |
| 256 |
#' lg <- get_logger("test")$ |
| 257 |
#' set_appenders(AppenderConsole$new())$ |
| 258 |
#' set_propagate(FALSE) |
| 259 |
#' |
| 260 |
#' lg$appenders[[1]]$set_layout(LayoutGlue$new()) |
| 261 |
#' lg$fatal("test") |
| 262 |
#' |
| 263 |
#' |
| 264 |
#' # All fields of the LogEvent are available, even custom ones |
| 265 |
#' lg$appenders[[1]]$layout$set_fmt( |
| 266 |
#' "{logger} {level_name}({level}) {caller}: {toupper(msg)} {{custom: {custom}}}" |
| 267 |
#' ) |
| 268 |
#' lg$fatal("test", custom = "foobar") |
| 269 |
#' lg$config(NULL) # reset logger config |
| 270 |
LayoutGlue <- R6::R6Class( |
| 271 |
"LayoutGlue", |
| 272 |
inherit = Layout, |
| 273 |
public = list( |
| 274 |
initialize = function( |
| 275 |
fmt = "{pad_right(colorize_levels(toupper(level_name)), 5)} [{timestamp}] {msg}" |
| 276 |
){ |
| 277 |
assert_namespace("glue") |
| 278 |
self$set_fmt(fmt) |
| 279 |
}, |
| 280 |
|
| 281 |
|
| 282 |
format_event = function( |
| 283 |
event |
| 284 |
){ |
| 285 |
op <- parent.env(event) |
| 286 |
on.exit(parent.env(event) <- op) |
| 287 |
parent.env(event) <- environment() |
| 288 |
unclass(glue::glue(get(".fmt", private), .envir = event)) |
| 289 |
}, |
| 290 |
|
| 291 |
|
| 292 |
set_fmt = function(x){ |
| 293 |
assert(is_scalar_character(x)) |
| 294 |
private$.fmt <- x |
| 295 |
invisible(self) |
| 296 |
}, |
| 297 |
|
| 298 |
|
| 299 |
set_colors = function(x){ |
| 300 |
assert( |
| 301 |
is.null(x) || is.list(x), |
| 302 |
"'colors' must either be NULL or a list of functions, not ", |
| 303 |
class_fmt(x) |
| 304 |
) |
| 305 |
private$.colors <- x |
| 306 |
invisible(self) |
| 307 |
}, |
| 308 |
|
| 309 |
|
| 310 |
toString = function() { |
| 311 |
paste(fmt_class(class(self)[[1]]), self$fmt) |
| 312 |
} |
| 313 |
), |
| 314 |
|
| 315 |
|
| 316 |
active = list( |
| 317 |
#' @field fmt A string that will be interpreted by [glue::glue()] |
| 318 |
fmt = function() private$.fmt |
| 319 |
), |
| 320 |
|
| 321 |
private = list( |
| 322 |
.fmt = NULL |
| 323 |
) |
| 324 |
) |