johnmyleswhite / log4r

A log4j derivative for R.

Home Page:http://www.johnmyleswhite.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Only last log saved to file when `append = FALSE` to `file_appender`

shgoke opened this issue · comments

Thanks for this easy-to-use package.

I want my log file to be overwritten every time I run a script. When I specify append = FALSE option to file_appender in a script with multiple logging requests and run it by Rscript, only the last log is recorded in the log file --- though I want all logs in the script to be recorded. Is that intended? I'm a novice in logging, so please forgive me if I'm asking a stupid question.

Minimal example (R 4.0.3): Name the following as test.R.

library(log4r)
logfile <- "tmp.log"
logger <- logger(appenders = file_appender(logfile, append = FALSE))

info(logger, "Execution started")
p <- 1 + 2 + 3
info(logger, paste0("Result is ", p))
info(logger, "Execution completed")

Run Rscript test.R. Resulting tmp.log:

INFO  [2020-12-19 17:02:41] Execution completed

Nope, that's definitely a bug.

In the meantime you can work around this by doing something like

library(log4r)
logfile <- "tmp.log"
file.create("tmp.log")
logger <- logger(appenders = file_appender(logfile, append = TRUE))

since file.create() should truncate the file if it exists.