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 or similar.
Usage
formatter_glue_or_sprintf(
msg,
...,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)Arguments
- msg
passed to
sprintfasfmtor handled as part of...inglue- ...
passed to
gluefor the text interpolation- .logcall
the logging call being evaluated (useful in formatters and layouts when you want to have access to the raw, unevaluated R expression)
- .topcall
R expression from which the logging function was called (useful in formatters and layouts to extract the calling function's name or arguments)
- .topenv
original frame of the
.topcallcalling function where the formatter function will be evaluated and that is used to look up thenamespaceas well vialogger:::top_env_name
Details
Note that this function tries to be smart when passing arguments to
glue and sprintf, but might fail with some edge cases, and
returns an unformatted string.
See also
This is a log_formatter(), for alternatives, see
formatter_paste(), formatter_sprintf(), formatter_glue(),
formatter_glue_safe(), formatter_logging(),
formatter_json(), formatter_pander() and skip_formatter()
for marking a string not to apply the formatter on it.
Examples
if (FALSE) { # \dontrun{
formatter_glue_or_sprintf("{a} + {b} = %s", a = 2, b = 3, 5)
formatter_glue_or_sprintf("{pi} * {2} = %s", pi * 2)
formatter_glue_or_sprintf("{pi} * {2} = {pi*2}")
formatter_glue_or_sprintf("Hi ", "{c('foo', 'bar')}, did you know that 2*4={2*4}")
formatter_glue_or_sprintf("Hi {c('foo', 'bar')}, did you know that 2*4={2*4}")
formatter_glue_or_sprintf("Hi {c('foo', 'bar')}, did you know that 2*4=%s", 2 * 4)
formatter_glue_or_sprintf("Hi %s, did you know that 2*4={2*4}", c("foo", "bar"))
formatter_glue_or_sprintf("Hi %s, did you know that 2*4=%s", c("foo", "bar"), 2 * 4)
} # }