The best of both words: using both formatter functions in your log
messages, which can be useful eg if you are migrating from
sprintf() formatted log messages to glue::glue() or similar.
formatter_glue_or_sprintf(
msg,
...,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)passed to sprintf() as fmt or handled as part of ...
in glue::glue()
passed to glue::glue() for the text interpolation
the logging call being evaluated (useful in formatters and layouts when you want to have access to the raw, unevaluated R expression)
R expression from which the logging function was called (useful in formatters and layouts to extract the calling function's name or arguments)
original frame of the .topcall calling function
where the formatter function will be evaluated and that is used
to look up the namespace as well via logger:::top_env_name
character vector
Note that this function tries to be smart when passing arguments to
glue::glue() and sprintf(), but might fail with some edge cases, and
returns an unformatted string.
Other log_formatters:
formatter_cli(),
formatter_glue(),
formatter_glue_safe(),
formatter_json(),
formatter_logging(),
formatter_pander(),
formatter_paste(),
formatter_sprintf()
formatter_glue_or_sprintf("{a} + {b} = %s", a = 2, b = 3, 5)
#> [1] "2 + 3 = 5"
formatter_glue_or_sprintf("{pi} * {2} = %s", pi * 2)
#> [1] "3.14159265358979 * 2 = 6.28318530717959"
formatter_glue_or_sprintf("{pi} * {2} = {pi*2}")
#> [1] "3.14159265358979 * 2 = 6.28318530717959"
formatter_glue_or_sprintf("Hi ", "{c('foo', 'bar')}, did you know that 2*4={2*4}")
#> [1] "Hi foo, did you know that 2*4=8" "Hi bar, did you know that 2*4=8"
formatter_glue_or_sprintf("Hi {c('foo', 'bar')}, did you know that 2*4={2*4}")
#> [1] "Hi foo, did you know that 2*4=8" "Hi bar, did you know that 2*4=8"
formatter_glue_or_sprintf("Hi {c('foo', 'bar')}, did you know that 2*4=%s", 2 * 4)
#> [1] "Hi foo, did you know that 2*4=8" "Hi bar, did you know that 2*4=8"
formatter_glue_or_sprintf("Hi %s, did you know that 2*4={2*4}", c("foo", "bar"))
#> [1] "Hi foo, did you know that 2*4=8" "Hi bar, did you know that 2*4=8"
formatter_glue_or_sprintf("Hi %s, did you know that 2*4=%s", c("foo", "bar"), 2 * 4)
#> [1] "Hi foo, did you know that 2*4=8" "Hi bar, did you know that 2*4=8"