Skip to content

Commit 0f23727

Browse files
committed
Various small updates to rawMsg handling
1 parent cadc0f5 commit 0f23727

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
`{knitr}` rendering process, log messages are now output to `stderr` instead
55
of `stdout` by default, to avoid polluting markdown documents (#62, thx @gadenbuie).
66

7-
* added hidden `.rawMsg` property to LogEvents to store message without
7+
* BREAKING: added `rawMsg` property to LogEvents to store message without
88
string interpolation (e.g. that still contains the placeholders from
9-
`sprintf()` or `glue()`) (#60)
9+
`sprintf()` or `glue()`). rawMessage will be added by default to json
10+
log files (#60)
1011

1112
* updated Readme
1213

R/LogEvent.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ LogEvent <- R6::R6Class(
108108
#' @field values `list`. All values stored in the `LogEvent`, including
109109
#' all *custom fields*, but not including `event$.logger`.
110110
values = function(){
111-
fixed_vals <- c("level", "timestamp", "logger", "caller", "msg")
111+
fixed_vals <- c("level", "timestamp", "logger", "caller", "msg", "rawMsg")
112112
custom_vals <- setdiff(
113113
names(get(".__enclos_env__", self)[["self"]]),
114-
c(".__enclos_env__", "level_name", "initialize", "clone", "values", "rawMsg",
115-
".logger")
114+
c(".__enclos_env__", "level_name", "initialize", "clone", "values", ".logger")
116115
)
117116
valnames <- union(fixed_vals, custom_vals) # to enforce order of fixed_vals
118117
mget(valnames, envir = self)
@@ -335,6 +334,7 @@ as_tibble.LogEvent <- function(
335334
#' multiple threads.}
336335
#' \item{`%c`}{the calling function}
337336
#' \item{`%m`}{the log message}
337+
#' \item{`%r`}{the raw log message (without string interpolation)
338338
#' \item{`%f`}{all custom fields of `x` in a pseudo-JSON like format that is
339339
#' optimized for human readability and console output}
340340
#' \item{`%j`}{all custom fields of `x` in proper JSON. This requires that you
@@ -433,7 +433,7 @@ format.LogEvent <- function(
433433
fmt,
434434
valid_tokens = paste0(
435435
"%",
436-
c("t", "p", "c", "m", "l", "L", "n", "f", "j", "k", "K", "g"))
436+
c("t", "p", "c", "m", "r", "l", "L", "n", "f", "j", "k", "K", "g"))
437437
)
438438

439439
# format
@@ -450,6 +450,7 @@ format.LogEvent <- function(
450450
"%K" = colorize_levels(lvls, colors, transform = function(.) toupper(strtrim(., 1))),
451451
"%t" = format(get("timestamp", envir = x), format = timestamp_fmt),
452452
"%m" = get("msg", envir = x),
453+
"%r" = get("rawMsg", envir = x),
453454
"%c" = get("caller", envir = x),
454455
"%g" = get("logger", envir = x),
455456
"%p" = Sys.getpid(),
@@ -597,4 +598,4 @@ tokenize_format <- function(
597598

598599
# globals --------------------------------------------------------
599600

600-
DEFAULT_FIELDS <- c("level", "timestamp", "logger", "caller", "msg")
601+
DEFAULT_FIELDS <- c("level", "timestamp", "logger", "caller", "msg", "rawMsg")

R/basic_config.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,20 @@ basic_config <- function(
9797
if (!is.null(file)){
9898
ext <- tools::file_ext(file)
9999

100-
if (identical(tolower(ext), "json")){
101-
stop(
102-
"Please use `.jsonl` and not `.json` as file extension for JSON log",
103-
"files. The reason is that that JSON files created",
104-
"by lgr are not true JSON files but JSONlines files.",
105-
"See https://jsonlines.org/ for more infos."
106-
)
100+
if (tolower(ext) %in% c("jsonl", "json")){
101+
102+
if (identical(tolower(ext), "json")){
103+
warning(
104+
"Please use `.jsonl` and not `.json` as file extension for JSON log ",
105+
"files. The reason is that that JSON files created ",
106+
"by lgr are not true JSON files but JSONlines files. ",
107+
"See https://jsonlines.org/ for more infos."
108+
)
109+
}
107110

108-
} else if (identical(tolower(ext), "jsonl")){
109-
if (!is.null(fmt) && !identical(fmt, default_fmt))
111+
if (!is.null(fmt) && !identical(fmt, default_fmt)){
110112
warning("`fmt` is ignored if `file` is a '.jsonl' file")
113+
}
111114

112115
l$add_appender(
113116
name = "file",

tests/testthat/test_Appender.R

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ event <- LogEvent$new(
66
level = 200L,
77
timestamp = structure(1541175573.9308, class = c("POSIXct", "POSIXt")),
88
caller = NA_character_,
9-
msg = "foo bar"
9+
msg = "foo bar",
10+
rawMsg = "foo raw"
1011
)
1112

1213

@@ -19,8 +20,6 @@ test_that("Appender: $append() works", {
1920
})
2021

2122

22-
23-
2423
test_that("Appender: $set_threshold() works", {
2524
app <- Appender$new()
2625

@@ -50,7 +49,6 @@ test_that("AppenderFile: logging with LayoutFormat", {
5049
})
5150

5251

53-
5452
test_that("AppenderFile: logging with LayoutJson", {
5553
tf <- tempfile()
5654
on.exit(unlink(tf))
@@ -68,7 +66,6 @@ test_that("AppenderFile: logging with LayoutJson", {
6866
})
6967

7068

71-
7269
test_that("AppenderFile: creates empty log file on init", {
7370
tf <- tempfile()
7471
on.exit(unlink(tf))
@@ -82,7 +79,6 @@ test_that("AppenderFile: creates empty log file on init", {
8279
})
8380

8481

85-
8682
test_that("AppenderFile: $show() works", {
8783
tf <- tempfile()
8884
on.exit(unlink(tf))
@@ -117,7 +113,6 @@ test_that("AppenderFile: $show() works", {
117113
})
118114

119115

120-
121116
test_that("AppenderFile$data throws an error", {
122117
tf <- tempfile()
123118
on.exit(unlink(tf))
@@ -136,9 +131,6 @@ test_that("AppenderFile$data throws an error", {
136131
})
137132

138133

139-
140-
141-
142134
test_that("AppenderFile: creates empty log file on init", {
143135
tf <- tempfile()
144136
on.exit(unlink(tf))
@@ -192,8 +184,6 @@ test_that("AppenderConsole: $append() works", {
192184
})
193185

194186

195-
196-
197187
test_that("AppenderConsole: $filter() works", {
198188
app1 <- AppenderConsole$new()
199189
expect_true(app1$filter(event))
@@ -240,6 +230,17 @@ test_that("AppenderConsole: chooses stderr by default when in knitr", {
240230
})
241231

242232

233+
test_that("AppenderConsole: rawMessage visible when set in layout", {
234+
235+
lo <- LayoutFormat$new("%m -- %r")
236+
app <- AppenderConsole$new(layout = lo)
237+
238+
expect_match(
239+
capture.output(app$append(event)),
240+
"foo bar -- foo raw")
241+
})
242+
243+
243244
# AppenderBuffer ----------------------------------------------------
244245
# Tests must be executed in sequence
245246
# setup

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

0 commit comments

Comments
 (0)