daroczig / logger

A lightweight, modern and flexible, log4j and futile.logger inspired logging utility for R
https://daroczig.github.io/logger
249 stars 38 forks source link

Problems with setting up appender_file or appender_tee using a variable to store the file name #124

Closed dbontemps closed 9 months ago

dbontemps commented 9 months ago

With R 4.1.2 and logger 0.2.2.

It seems that if you set up you appender_file using a variable to store the file name, it is not correctly set up unless a log is committed before the variable is deleted.

This can also cause problem when setting up a logging system inside an auxiliary "log_init' function.

Here some examples in my Rstudio R console:

> library(logger)
> myfile = "essai.log"
> log_appender(appender_tee(myfile))
> rm(myfile)
> log_info("1")
INFO [2023-10-15 17:24:39] 1
Erreur dans force(file) : objet 'myfile' introuvable
> for(l in readLines("essai.log")) cat(paste(l,"\n"))
Erreur dans file(con, "r") : impossible d'ouvrir la connexion
De plus : Message d'avis :
Dans file(con, "r") :
  impossible d'ouvrir le fichier 'essai.log' : Aucun fichier ou dossier de ce type
> myfile = "essai.log"
> log_appender(appender_tee(myfile))
> log_info("1")
INFO [2023-10-15 17:25:41] 1
> rm(myfile)
> log_info("2")
INFO [2023-10-15 17:25:53] 2
> for(l in readLines("essai.log")) cat(paste(l,"\n"))
INFO [2023-10-15 17:25:41] 1 
INFO [2023-10-15 17:25:53] 2 

It may seem secondary but it is probably related : log_appender() does not give a reliable information about the log setting. I was not able to get a precise report about it.

> log_appender()
appender_tee(file = myfile)
dbontemps commented 9 months ago

Sorry, I did not master the process, my code example does not distinguish between code and outputs...

daroczig commented 9 months ago

Thanks for raising this -- seems like the file arg of appender_tee was not forced. Please try the dev version of the pkg, and report back.