Closed patrickvdb closed 11 months ago
I think the root of the above is this:
> glue(1:2)
Error: All unnamed arguments must be length 1
So although glue
can take multiple arguments and will concat if needed, e.g.:
> glue('foo', 'bar')
foobar
Passing a character vector as per above will not work.
If you want to pass character vectors instead of strings (to be interpolated), then indeed a new/another formatted is needed, e.g.
> log_formatter(formatter_paste)
> log_info(1:2)
INFO [2023-08-19 12:23:55] 1
INFO [2023-08-19 12:23:55] 2
Or a custom one, like you suggested above making use of glue
after concatenating internally.
Overall, I don't think it's a bug in formatter_glue
as it works as intended -- passing log msg and variables for interpolation to glue
. So thus I'm closing this ticket, but feel free to open a PR to add a new formatter.
Perhaps it was not clear why this can be a problem. It's not a bug if it works as expected, I agree, but we don't fully control warning messages other packages print and with log_warnings()
this results in code failing just because the log functions fails.
See mwe example below.
Perhaps I'll make a pull request for formatter_glue_safe()
that defaults to another formatter for the log instead of stopping.
library(logger)
library(forcats)
log_warnings()
fct_explicit_na(factor(c("a", NA)))
# anything after this is not run: glue+logger error stops the script entirely even though it was merely warning
I noticed there already is a pull request for a formatter_glue_safe()
that uses glue::glue_safe()
. Maybe I'll figure out something different. Perhaps a fallback mechanic to the paste formatter?
I would also like to note that I was was mistaken before in that formatter_glue_or_sprintf()
does not result in an error in the above case.
We ran into some warnings with length 2 that resulted in glue returning an error. The solution I found right now it is to use a custom formatter that unlists the input for glue.
I am unsure if this would be a drop+replace for the current formatter (
formatter_glue()
).NB The same problem occured with
formatter_glue_or_sprintf()