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

Issues when using logger in r-markdown when initializing log_errors() #111

Closed miyesa closed 1 year ago

miyesa commented 1 year ago

I created a function to initialize logging using logger package and to save the logs entries in a temporary file in the tempdir() given by R.

init_logger <- function() {
  library(logger)
  temporary_directory <- tempdir()
  date_time_current <- format(Sys.time(), "log_%Y_%m_%d_%H_%M_%S_")
  tmp <- tempfile(pattern = date_time_current, tmpdir = temporary_directory, fileext = '.txt')
  configuration_logger(tmp)
  log_info('Logging starting:')
  return(tmp)
  }

configuration_logger <- function(tmp){
  log_appender(appender_file(tmp))
  log_threshold(TRACE)
  log_formatter(formatter_paste)
  log_layout(layout_simple)
  log_errors()

}

The function above initializes the log and I am calling this mypackage::init_logger() in another function mypackage::get_data(client, dimensions, metrics, start_date, end_date, ...) where I want to capture certain information as well as any errors that might occur, therefore the log_errors()is crucial in order to debug if anything goes wrong. Everything works fine if this function is called in a regular R-script, but it does not work the same way in a R-markdown.

If I force an error in a markdown:

```{r}
library(mypackage)
library(magrittr)
clients <- "client_x"
end_date <- Sys.Date() - 1
# FORCING ERROR HERE... 
start_date <- "not_defined"
#start_date <- Sys.Date() - 30
new_data <- get_data(client = clients, 
                     start_date = start_date,
                     end_date = end_date,
                     dimension, metrics, ...)

In this case I get the following error in the console and no log-file gets generated.

Error in globalCallingHandlers(error = function(m) { : 
  should not be called with handlers on the stack

Now if I have the same lines of code but instead in just a regular .R script file the actual log files gets generated and the error I am expecting also is recorded in the log file created.

INFO [2022-07-26 11:26:51] Logging starting:
INFO [2022-07-26 11:26:52] Starting Function: -get_data()-. File: -data.R-
ERROR [2022-07-26 11:26:59] nanodbc/nanodbc.cpp...

I know for certain that the cause of this issue is when log_errors() is called in the function configuration_logger() that is used in init_logger(). I tried removing this line of code and the log files get generated in both the .Rmd rmarkdown and .R script when calling the same function, but then any errors occurring do not get recorded in the log file but they are just printed in the console instead.

Anyone knows why this is occurring in r-markdowns only when log_errors() is initialized and if there is a possible solution to it since I would like to record any errors that might occur in my function in the log file that gets created when using r-markdowns.

miyesa commented 1 year ago

I just found this related to the issue.

104 70

Therefore duplicated issue.