Skip to content

Commit 68ef976

Browse files
committed
add hidden .rawMsg property to LogEvent for storing msg without string interpolation
1 parent fe6faab commit 68ef976

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
* added `as_LogEvent()` to coerce various event-like objects to `LogEvents`
1515

16+
* added hidden `.rawMsg` property to LogEvents to store message without
17+
string interpolation (e.g. that still contains the placeholders from
18+
`sprintf()` or `glue()`)
19+
1620

1721
# lgr 0.4.3
1822

R/LogEvent.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ LogEvent <- R6::R6Class(
5353
timestamp = Sys.time(),
5454
caller = NA,
5555
msg = NA,
56+
.rawMsg = msg,
5657
...
5758
){
5859
assert(inherits(logger, "Logger"))
@@ -64,6 +65,7 @@ LogEvent <- R6::R6Class(
6465
assign("timestamp", timestamp, self)
6566
assign("caller", caller, self)
6667
assign("msg", msg, self)
68+
assign(".rawMsg", .rawMsg, self)
6769

6870
# custom values
6971
if (!missing(...)){
@@ -94,18 +96,22 @@ LogEvent <- R6::R6Class(
9496

9597
#' @field .logger [Logger]. A reference to the Logger that created the
9698
#' event (equivalent to `get_logger(event$logger)`).
97-
.logger = NULL
99+
.logger = NULL,
100+
101+
#' @field .rawMsg `character`. The raw log message without string
102+
#' interpolation.
103+
.rawMsg = NULL
98104
),
99105

100106
active = list(
101107

102108
#' @field values `list`. All values stored in the `LogEvent`, including
103-
#' all *custom fields*, but not including `event$.logger`.
109+
#' all *custom fields*, but not including `event$.logger` and `event$.rawMsg`.
104110
values = function(){
105111
fixed_vals <- c("level", "timestamp", "logger", "caller", "msg")
106112
custom_vals <- setdiff(
107113
names(get(".__enclos_env__", self)[["self"]]),
108-
c(".__enclos_env__", "level_name", "initialize", "clone", "values",
114+
c(".__enclos_env__", "level_name", "initialize", "clone", "values", ".rawMsg",
109115
".logger")
110116
)
111117
valnames <- union(fixed_vals, custom_vals) # to enforce order of fixed_vals

R/Logger.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,16 @@ Logger <- R6::R6Class(
226226
}
227227

228228
force(caller)
229+
rawMsg <- msg
229230

230231
if (missing(...)){
231232
vals <- list(
232233
logger = self,
233234
level = level,
234235
timestamp = timestamp,
235236
caller = caller,
236-
msg = msg
237+
msg = msg,
238+
.rawMsg = rawMsg
237239
)
238240
} else {
239241
dots <- list(...)
@@ -245,7 +247,8 @@ Logger <- R6::R6Class(
245247
level = level,
246248
timestamp = timestamp,
247249
caller = caller,
248-
msg = msg
250+
msg = msg,
251+
.rawMsg = rawMsg
249252
)
250253
} else {
251254
not_named <- vapply(names(dots), is_blank, TRUE, USE.NAMES = FALSE)
@@ -259,7 +262,8 @@ Logger <- R6::R6Class(
259262
level = level,
260263
timestamp = timestamp,
261264
caller = caller,
262-
msg = msg
265+
msg = msg,
266+
.rawMsg = rawMsg
263267
),
264268
dots[!not_named]
265269
)
@@ -896,6 +900,7 @@ LoggerGlue <- R6::R6Class(
896900
}
897901
})
898902

903+
rawMsg <- dots[[1]]
899904
msg <- do.call(glue::glue, args = c(dots, list(.envir = .envir)))
900905

901906
# Check if LogEvent should be created
@@ -914,7 +919,8 @@ LoggerGlue <- R6::R6Class(
914919
level = level,
915920
timestamp = timestamp,
916921
caller = caller,
917-
msg = msg
922+
msg = msg,
923+
.rawMsg = rawMsg
918924
)
919925
} else {
920926
dots <- list(...)
@@ -926,7 +932,8 @@ LoggerGlue <- R6::R6Class(
926932
level = level,
927933
timestamp = timestamp,
928934
caller = caller,
929-
msg = msg
935+
msg = msg,
936+
.rawMsg = rawMsg
930937
),
931938
dots[custom_fields]
932939
)

man/LogEvent.Rd

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

tests/testthat/test_Logger.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
context("Logger")
22

33

4+
# Logger ------------------------------------------------------------------
5+
6+
47

58
test_that("logging conditions works", {
69
e <- error("blahblah")
@@ -379,11 +382,17 @@ test_that("$config works with lists", {
379382

380383

381384

385+
# Multi-Logger tests -------------------------------------------------------------
382386

383387
test_that("Logger$log() dispatches to all appenders, even if some throw an error", {
384388
ln <- Logger$new("normal", propagate = FALSE)
385389
lg <- LoggerGlue$new("glue", propagate = FALSE)
386390

391+
on.exit({
392+
ln$config(NULL)
393+
lg$config(NULL)
394+
})
395+
387396
AppErr <- R6::R6Class(
388397
inherit = AppenderConsole,
389398
public = list(
@@ -416,6 +425,11 @@ test_that("Logger error contains useful call object", {
416425
l <- get_logger("test")
417426
g <- get_logger_glue("testglue")
418427

428+
on.exit({
429+
l$config(NULL)
430+
g$config(NULL)
431+
})
432+
419433
expect_warning(l$info("this will fail", e = stop()), "l\\$info")
420434
expect_warning(g$info("this will fail", e = stop()), "g\\$info")
421435
})
@@ -428,6 +442,11 @@ test_that("Appender error contains useful call object", {
428442
l <- get_logger("test")$set_propagate(FALSE)
429443
g <- get_logger_glue("testglue")$set_propagate(FALSE)
430444

445+
on.exit({
446+
l$config(NULL)
447+
g$config(NULL)
448+
})
449+
431450
AppenderFail <- R6::R6Class(
432451
"AppenderFail",
433452
inherit = Appender,
@@ -445,3 +464,24 @@ test_that("Appender error contains useful call object", {
445464
expect_warning(g$info("this will fail"), ".*AppenderFail.*g\\$info")
446465
})
447466

467+
468+
469+
470+
test_that(".rawMsg works", {
471+
l <- get_logger("test")$set_propagate(FALSE)
472+
g <- get_logger_glue("testglue")$set_propagate(FALSE)
473+
474+
on.exit({
475+
l$config(NULL)
476+
g$config(NULL)
477+
})
478+
479+
l$info("foo %s", "bar")
480+
g$fatal("hash {x}", x = "baz")
481+
482+
expect_identical(l$last_event$msg, "foo bar")
483+
expect_identical(l$last_event$.rawMsg, "foo %s")
484+
485+
expect_identical(as.character(g$last_event$msg), "hash baz")
486+
expect_identical(g$last_event$.rawMsg, "hash {x}")
487+
})

0 commit comments

Comments
 (0)