Skip to content

Commit a04f8cf

Browse files
committed
Add support for excluded_fields to Layout, and exclude rawMsg by default from LayotJson
1 parent 59d4bf9 commit a04f8cf

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
* Support transformers for `LoggerGlue` (see `?glue::glue`) (#51)
1616

17-
* Add support for `excluded_properties` to LayoutJson (and exclude rawMsg by
18-
default)
17+
* Add support for `excluded_fields` to `Layout`, and exclude rawMsg by
18+
default from `LayotJson`
1919

2020
* updated README
2121

R/Layout.R

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,29 @@ Layout <- R6::R6Class(
4242
toString(event)
4343
},
4444

45-
toString = function() fmt_class(class(self)[[1]])
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
4668
)
4769
)
4870

@@ -86,12 +108,14 @@ LayoutFormat <- R6::R6Class(
86108
fmt = "%L [%t] %m %j",
87109
timestamp_fmt = "%Y-%m-%d %H:%M:%OS3",
88110
colors = NULL,
89-
pad_levels = "right"
111+
pad_levels = "right",
112+
excluded_fields = NULL
90113
){
91114
self$set_fmt(fmt)
92115
self$set_timestamp_fmt(timestamp_fmt)
93116
self$set_colors(colors)
94117
self$set_pad_levels(pad_levels)
118+
self$set_excluded_fields(excluded_fields)
95119
},
96120

97121
#' @description Format a LogEvent
@@ -104,7 +128,8 @@ LayoutFormat <- R6::R6Class(
104128
fmt = private$.fmt,
105129
timestamp_fmt = private$.timestamp_fmt,
106130
colors = private$.colors,
107-
pad_levels = private$.pad_levels
131+
pad_levels = private$.pad_levels,
132+
excluded_fields = private$.excluded_fields
108133
)
109134
},
110135

@@ -335,16 +360,16 @@ LayoutJson <- R6::R6Class(
335360
initialize = function(
336361
toJSON_args = list(auto_unbox = TRUE),
337362
timestamp_fmt = NULL,
338-
excluded_properties = "rawMsg"
363+
excluded_fields = "rawMsg"
339364
){
340365
self$set_toJSON_args(toJSON_args)
341366
self$set_timestamp_fmt(timestamp_fmt)
342-
self$set_excluded_properties(excluded_properties)
367+
self$set_excluded_fields(excluded_fields)
343368
},
344369

345370
format_event = function(event) {
346371
vals <- get("values", event)
347-
vals <- vals[!names(vals) %in% self$excluded_properties]
372+
vals <- vals[!names(vals) %in% self$excluded_fields]
348373
fmt <- get("timestamp_fmt", self)
349374

350375
if (!is.null(fmt)){
@@ -384,12 +409,6 @@ LayoutJson <- R6::R6Class(
384409
invisible(self)
385410
},
386411

387-
set_excluded_properties = function(x){
388-
assert(is.null(x) || is.character(x))
389-
private$.excluded_properties <- x
390-
invisible(self)
391-
},
392-
393412
# . . methods ----------------------------------------------------------------
394413

395414
toString = function() {
@@ -435,20 +454,13 @@ LayoutJson <- R6::R6Class(
435454
#' @field timestamp_fmt Used by `$format_event()` to format timestamps.
436455
timestamp_fmt = function() {
437456
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)
444457
}
445458
),
446459

447460
# . . private --------------------------------------------------------------
448461
private = list(
449462
.toJSON_args = NULL,
450-
.timestamp_fmt = NULL,
451-
.excluded_properties = NULL
463+
.timestamp_fmt = NULL
452464
)
453465
)
454466

R/LogEvent.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ format.LogEvent <- function(
401401
colors = NULL,
402402
log_levels = getOption("lgr.log_levels"),
403403
pad_levels = "right",
404+
excluded_fields = NULL,
404405
...
405406
){
406407
stopifnot(
@@ -454,8 +455,8 @@ format.LogEvent <- function(
454455
"%c" = get("caller", envir = x),
455456
"%g" = get("logger", envir = x),
456457
"%p" = Sys.getpid(),
457-
"%f" = format_custom_fields(get_custom_fields(x), color = length(colors)),
458-
"%j" = format_custom_fields_json(get_custom_fields(x)),
458+
"%f" = format_custom_fields(get_custom_fields(x, excluded_fields), color = length(colors)),
459+
"%j" = format_custom_fields_json(get_custom_fields(x, excluded_fields)),
459460
tokens[[i]]
460461
)
461462
}
@@ -515,8 +516,8 @@ do_box_if <- function(x, predicate, except, boxer = list){
515516

516517

517518

518-
get_custom_fields <- function(x){
519-
x$values[!names(x$values) %in% DEFAULT_FIELDS]
519+
get_custom_fields <- function(x, excluded_fields = NULL){
520+
x$values[!names(x$values) %in% c(DEFAULT_FIELDS, excluded_fields)]
520521
}
521522

522523

tests/testthat/test_Layout.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ test_that("LayoutJson works as expected", {
8080
})
8181

8282

83-
test_that("LayoutJson `exclude_properties` works as expected", {
84-
lo <- LayoutJson$new(excluded_properties = NULL)
83+
test_that("LayoutJson `excluded_fields` works as expected", {
84+
lo <- LayoutJson$new(excluded_fields = NULL)
8585

8686
x <- tevent$clone()
8787
x$foo <- "bar"

tests/testthat/test_read_json_log.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_that("read_json_lines() works as expected", {
2020

2121
tres <- read_json_lines(tf)
2222

23-
expect_identical(names(tres), c("level", "timestamp", "logger", "caller", "msg", "rawMsg"))
23+
expect_identical(names(tres), c("level", "timestamp", "logger", "caller", "msg"))
2424
expect_true(all(tres$level == seq(100, 600, by = 100)))
2525
file.remove(tf)
2626
})

0 commit comments

Comments
 (0)