Skip to content

Commit 59d4bf9

Browse files
committed
Add support for excluded_properties to LayoutJson (and exclude rawMsg by
default)
1 parent b6fd07f commit 59d4bf9

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
Before, `NULL` values would have resulted in empty log messages. (#51)
1414

1515
* Support transformers for `LoggerGlue` (see `?glue::glue`) (#51)
16+
17+
* Add support for `excluded_properties` to LayoutJson (and exclude rawMsg by
18+
default)
1619

1720
* updated README
1821

R/Layout.R

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Layout ------------------------------------------------------------------
2+
3+
14
#' Abstract Class for Layouts
25
#'
36
#' [Appenders] pass [LogEvents][LogEvent] to a Layout which formats it for
@@ -331,14 +334,17 @@ LayoutJson <- R6::R6Class(
331334

332335
initialize = function(
333336
toJSON_args = list(auto_unbox = TRUE),
334-
timestamp_fmt = NULL
337+
timestamp_fmt = NULL,
338+
excluded_properties = "rawMsg"
335339
){
336340
self$set_toJSON_args(toJSON_args)
337341
self$set_timestamp_fmt(timestamp_fmt)
342+
self$set_excluded_properties(excluded_properties)
338343
},
339344

340345
format_event = function(event) {
341346
vals <- get("values", event)
347+
vals <- vals[!names(vals) %in% self$excluded_properties]
342348
fmt <- get("timestamp_fmt", self)
343349

344350
if (!is.null(fmt)){
@@ -360,6 +366,9 @@ LayoutJson <- R6::R6Class(
360366
invisible(self)
361367
},
362368

369+
370+
# . . setters -------------------------------------------------------------
371+
363372
#' @description Set a format that this Layout will apply to timestamps.
364373
#'
365374
#' @param x
@@ -375,6 +384,14 @@ LayoutJson <- R6::R6Class(
375384
invisible(self)
376385
},
377386

387+
set_excluded_properties = function(x){
388+
assert(is.null(x) || is.character(x))
389+
private$.excluded_properties <- x
390+
invisible(self)
391+
},
392+
393+
# . . methods ----------------------------------------------------------------
394+
378395
toString = function() {
379396
fmt_class(class(self)[[1]])
380397
},
@@ -406,23 +423,40 @@ LayoutJson <- R6::R6Class(
406423

407424
),
408425

426+
427+
# . . active fields ------------------------------------------------------
428+
409429
active = list(
410430
#' @field toJSON_args a list of values passed on to [jsonlite::toJSON()]
411-
toJSON_args = function() get(".toJSON_args", private),
431+
toJSON_args = function() {
432+
get(".toJSON_args", private)
433+
},
412434

413435
#' @field timestamp_fmt Used by `$format_event()` to format timestamps.
414-
timestamp_fmt = function() get(".timestamp_fmt", private)
436+
timestamp_fmt = function() {
437+
get(".timestamp_fmt", private)
438+
},
439+
440+
#' @field excluded_properties Used by `$format_event()` to exclude selected
441+
#' properties before serialization
442+
excluded_properties = function() {
443+
get(".excluded_properties", private)
444+
}
415445
),
416446

447+
# . . private --------------------------------------------------------------
417448
private = list(
418449
.toJSON_args = NULL,
419-
.timestamp_fmt = NULL
450+
.timestamp_fmt = NULL,
451+
.excluded_properties = NULL
420452
)
421453
)
422454

423455

424456

425457

458+
# utils -------------------------------------------------------------------
459+
426460
fmt_timestamp = function(x, fmt){
427461
if (is.character(fmt)){
428462
format(x, fmt)

tests/testthat/test_Layout.R

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ tevent <- LogEvent$new(
66
level = 200L,
77
timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"),
88
caller = NA_character_,
9-
msg = "foo bar"
9+
msg = "foo bar",
10+
rawMsg = "foo raw"
1011
)
1112

1213

@@ -67,13 +68,38 @@ test_that("LayoutJson works as expected", {
6768
json <- lo$format_event(x)
6869
tres <- jsonlite::fromJSON(json)
6970

71+
expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo") # rawMsg is excluded by default
72+
73+
tres[sapply(tres, is.null)] <- NA_character_
74+
expect_setequal(expected_values, names(tres))
75+
expect_identical(tres[["level"]], eres[["level"]])
76+
expect_identical(tres[["msg"]], eres[["msg"]])
77+
expect_identical(tres[["caller"]], eres[["caller"]])
78+
expect_equal(as.POSIXct(tres[["timestamp"]], tz = "UTC"), eres[["timestamp"]], tolerance = 1)
79+
expect_equal(tres[["foo"]], "bar")
80+
})
81+
82+
83+
test_that("LayoutJson `exclude_properties` works as expected", {
84+
lo <- LayoutJson$new(excluded_properties = NULL)
85+
86+
x <- tevent$clone()
87+
x$foo <- "bar"
88+
89+
eres <- x$values
90+
json <- lo$format_event(x)
91+
tres <- jsonlite::fromJSON(json)
92+
93+
expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo", "rawMsg")
94+
7095
tres[sapply(tres, is.null)] <- NA_character_
71-
expect_setequal(c(names(eres), "foo"), names(tres))
96+
expect_setequal(expected_values, names(tres))
7297
expect_identical(tres[["level"]], eres[["level"]])
7398
expect_identical(tres[["msg"]], eres[["msg"]])
7499
expect_identical(tres[["caller"]], eres[["caller"]])
75100
expect_equal(as.POSIXct(tres[["timestamp"]], tz = "UTC"), eres[["timestamp"]], tolerance = 1)
76101
expect_equal(tres[["foo"]], "bar")
102+
expect_equal(tres[["rawMsg"]], "foo raw")
77103
})
78104

79105

0 commit comments

Comments
 (0)