Closed alevax closed 2 years ago
I suppose this happens with layout_glue_colors
-- in that case, I'd not suggest using appender_tee
, but instead set up layout_glue_colors
for the console and something else (e.g. layout_glue
) for the file using index
. See https://daroczig.github.io/logger/articles/customize_logger.html#stacking-loggers for more details, and feel free to reopen if you think that doesn't address your problem.
Thanks for pointing it out. I used the namespace parameter and it looks like it works for the purpose of having a colored output in the console and plain text in the log file. Here is the code I used:
log_threshold(DEBUG,namespace = "file")
log_appender(appender = appender_file("log.txt),namespace = "file")
log_threshold(DEBUG,namespace = "console")
log_formatter(formatter_glue,namespace = "console")
log_layout(layout_glue_colors,namespace = "console")
log_warn('Loading Data',namespace="console")
log_warn('Loading Data',namespace="file")
However, I don't know if what I expect is a missing feature or me not knowing how to do it, but I guess it would be nice to have one log function that writes both to file and console (colored). With appender_tee it looks like it is possible to do so, but without the coloring output to console, since it would add to file a lot of unwanted symbols.
Is there a way to reach this goal or is the feature not implemented (i.e., one function to log both file and colored console)?
Yeah, it's implemented: look at the index
parameter instead of the namespace
.
Wonderful! Thanks a lot, @daroczig.
It was very straightforward:
library(logger)
log_threshold(DEBUG)
log_threshold(DEBUG,index=2)
log_appender(appender = appender_file("log.txt"),index=2)
log_layout(layout_glue_colors)
log_warn('Ciao Bello')
When I run this basic code to log both console and sdtout, the txt file contains also other symbols used to color output in console:
[1m[0mINFO[0m[1m[23m[24m[27m[28m[29m[39m[49m[0m[0m[1m[23m[24m[27m[28m[29m[39m[49m[22m [[3m2022-01-18 17:55:05[23m] [38;5;246mLoading Data[39m[0m[0m[22m[23m[24m[27m[28m[29m[39m[49m
When instead I'd like to see only the text.
This is the code: