From 5229292ade6e14223632664d9310b010dbc6142d Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:05:53 +0900 Subject: [PATCH 1/4] feat: enable renaming meta fields --- R/layouts.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/layouts.R b/R/layouts.R index bd852dc..e5e19c2 100644 --- a/R/layouts.R +++ b/R/layouts.R @@ -317,6 +317,12 @@ layout_json_parser <- function(fields = default_fields()) { .topenv = .topenv ) meta <- mget(fields, meta) + field_names <- names(fields) + if (!is.null(field_names)) { + norename <- field_names == "" + field_names[norename] <- fields[norename] + meta <- setNames(meta, field_names) + } msg <- jsonlite::fromJSON(msg) jsonlite::toJSON(c(meta, msg), auto_unbox = TRUE, null = "null") From 67b5e12df9e4dc7903adb039d884370b3bedb322 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:14:54 +0900 Subject: [PATCH 2/4] doc: enable renaming meta fields --- R/layouts.R | 2 +- man/layout_json_parser.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/layouts.R b/R/layouts.R index e5e19c2..6427623 100644 --- a/R/layouts.R +++ b/R/layouts.R @@ -283,7 +283,7 @@ layout_json <- function(fields = default_fields()) { #' Generate log layout function rendering JSON after merging meta #' fields with parsed list from JSON message #' @param fields character vector of field names to be included in the -#' JSON +#' JSON. If named, the names will be used as field names in the JSON. #' @export #' @note This functionality depends on the \pkg{jsonlite} package. #' @family log_layouts diff --git a/man/layout_json_parser.Rd b/man/layout_json_parser.Rd index 390ea59..8d48990 100644 --- a/man/layout_json_parser.Rd +++ b/man/layout_json_parser.Rd @@ -9,7 +9,7 @@ layout_json_parser(fields = default_fields()) } \arguments{ \item{fields}{character vector of field names to be included in the -JSON} +JSON. If named, the names will be used as field names in the JSON.} } \description{ Generate log layout function rendering JSON after merging meta From 38e15522626edc08cea29feed7b52fd86cfd0c6a Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 25 Jan 2025 22:01:38 +0900 Subject: [PATCH 3/4] doc: add example of renaming meta fields --- R/layouts.R | 8 ++++++++ man/layout_json_parser.Rd | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/R/layouts.R b/R/layouts.R index 6427623..adc8cd4 100644 --- a/R/layouts.R +++ b/R/layouts.R @@ -297,6 +297,14 @@ layout_json <- function(fields = default_fields()) { #' #' log_layout(layout_json_parser(fields = c("time", "node"))) #' log_info(cars = row.names(mtcars), species = unique(iris$Species)) +#' +#' log_layout(layout_json_parser(fields = c(timestamp = "time", "node"))) +#' log_info( +#' message = paste( +#' "Compared to the previous example, +#' the 'time' field is renamed to 'timestamp'" +#' ) +#' ) #' \dontshow{logger:::namespaces_set(old)} layout_json_parser <- function(fields = default_fields()) { force(fields) diff --git a/man/layout_json_parser.Rd b/man/layout_json_parser.Rd index 8d48990..3348973 100644 --- a/man/layout_json_parser.Rd +++ b/man/layout_json_parser.Rd @@ -28,6 +28,14 @@ log_info(everything = 42) log_layout(layout_json_parser(fields = c("time", "node"))) log_info(cars = row.names(mtcars), species = unique(iris$Species)) + +log_layout(layout_json_parser(fields = c(timestamp = "time", "node"))) +log_info( + message = paste( + "Compared to the previous example, + the 'time' field is renamed to 'timestamp'" + ) +) \dontshow{logger:::namespaces_set(old)} } \seealso{ From eb536f87ab176aa2ab4ac83eeabc3dee5bf4a159 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 25 Jan 2025 22:07:20 +0900 Subject: [PATCH 4/4] test: rename meta fields --- tests/testthat/test-layouts.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-layouts.R b/tests/testthat/test-layouts.R index a42c11e..e8d6e1d 100644 --- a/tests/testthat/test-layouts.R +++ b/tests/testthat/test-layouts.R @@ -41,6 +41,11 @@ test_that("JSON parser layout", { expect_output(log_info(skip_formatter('{"x": 4}')), '{"x":4}', fixed = TRUE) }) +test_that("JSON parser layout can be renamed", { + local_test_logger(layout = layout_json_parser(c(LEVEL = "level"))) + expect_output(log_info(skip_formatter('{"x": 4}')), '{"LEVEL":"INFO","x":4}', fixed = TRUE) +}) + test_that("must throw errors", { skip_if_not(getRversion() >= "4.3") # error call changed