Skip to content

Commit 430818a

Browse files
committed
add helpful warning when trying to use $msg argument with LoggerGlue
1 parent 8fe0c76 commit 430818a

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

R/Logger.R

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -883,20 +883,33 @@ LoggerGlue <- R6::R6Class(
883883
)
884884

885885
dots <- list(...)
886-
sel <- names(dots) == ""
886+
887+
if ("msg" %in% names(dots)){
888+
warning(
889+
"LoggerGlue does not support a `$msg` argument. Please use unnamed ",
890+
"text strings for construction your message like in `glue::glue()`.",
891+
call. = FALSE
892+
)
893+
names(dots)[names(dots) == "msg"] <- ""
894+
}
895+
896+
# construct msg
897+
dots_msg <- dots # because we need the original dots later again
898+
899+
sel <- names(dots_msg) == ""
887900
if (!length(sel)){
888-
sel <- seq_along(dots)
901+
sel <- seq_along(dots_msg)
889902
}
890903

891-
dots[sel] <- lapply(dots[sel], function(e){
904+
dots_msg[sel] <- lapply(dots_msg[sel], function(e){
892905
if (inherits(e, "condition")){
893906
conditionMessage(e)
894907
} else {
895908
as.character(e)
896909
}
897910
})
898911

899-
msg <- do.call(glue::glue, args = c(dots, list(.envir = .envir)))
912+
msg <- do.call(glue::glue, args = c(dots_msg, list(.envir = .envir)))
900913

901914
# Check if LogEvent should be created
902915
if (
@@ -909,15 +922,8 @@ LoggerGlue <- R6::R6Class(
909922
force(caller)
910923

911924
if (missing(...)){
912-
vals <- list(
913-
logger = self,
914-
level = level,
915-
timestamp = timestamp,
916-
caller = caller,
917-
msg = msg
918-
)
925+
stop("No log message or structured logging fields supplied")
919926
} else {
920-
dots <- list(...)
921927
custom_fields <- !(grepl("^\\.", names(dots)) | is_blank(names(dots)))
922928

923929
vals <- c(

tests/testthat/test_Logger.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ test_that("Appender error contains useful call object", {
428428
l <- get_logger("test")$set_propagate(FALSE)
429429
g <- get_logger_glue("testglue")$set_propagate(FALSE)
430430

431+
on.exit({
432+
l$config(NULL)
433+
g$config(NULL)
434+
})
435+
431436
AppenderFail <- R6::R6Class(
432437
"AppenderFail",
433438
inherit = Appender,
@@ -445,3 +450,19 @@ test_that("Appender error contains useful call object", {
445450
expect_warning(g$info("this will fail"), ".*AppenderFail.*g\\$info")
446451
})
447452

453+
454+
455+
456+
457+
test_that("Logger$log 'msg' argument works as expected", {
458+
l <- get_logger("test")$set_propagate(FALSE)
459+
g <- get_logger_glue("testglue")$set_propagate(FALSE)
460+
461+
on.exit({
462+
l$config(NULL)
463+
g$config(NULL)
464+
})
465+
466+
expect_silent(l$log(level = "fatal", msg = "test"))
467+
expect_warning(g$log(level = "fatal", msg = "test"), "does not support")
468+
})

0 commit comments

Comments
 (0)