daroczig / logger

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

Feature request: integration with tryCatch #69

Closed edgBR closed 2 years ago

edgBR commented 3 years ago

Dear colleagues,

Following the best practices of our Software developer colleagues the data scientist team where I am working for have started to implement trycatch + standard logging to debug our containerized code faster.

I have been experimenting with logger and so far the experience is awesome!. However it is a bit tricky to combine with trycatch. For example:

s3upload <- function(file_path, file_name_s3, target_bucket, region_name) {
  tryCatch(
    {

      s3save(file = paste0(file_path, file_name_s3),
                 bucket = target_bucket,
                 object = paste0("example/", 
                                 file_name),
                 multipart = TRUE,
                 show_progress = TRUE)
      log_info(paste0("Data uploaded to s3!: ", file_name))
    }, error = function(e) {
      message(e)
    }, warning = function(w) {
      message(w)
    }
  )
}

When running this (which btw is wrong code) I get the following error:

> s3upload(file_path = argument_parser$output_path, 
+          file_name_s3 = file_name,
+          target_bucket = argument_parser$bucket, 
+          region_name = argument_parser$region)
formal argument "file" matched by multiple actual arguments

When running with logger:

s3upload <- function(file_path, file_name_s3, target_bucket, region_name) {
  tryCatch(
    {

      s3save(file = paste0(file_path, file_name_s3),
                 bucket = target_bucket,
                 object = paste0("example/", 
                                 file_name),
                 multipart = TRUE,
                 show_progress = TRUE)
      log_info(paste0("Data uploaded to s3!: ", file_name))
    }, error = function(e) {
      log_error(e)
    }, warning = function(w) {
      log_warn(w)
    }
  )
}

I get a very cryptic error.

> s3upload(file_path = argument_parser$output_path, 
+          file_name_s3 = file_name,
+          target_bucket = argument_parser$bucket, 
+          region_name = argument_parser$region)
 Error: All unnamed arguments must be length 1 

I am misunderstanding the usage of logger in this situations? Should I be using trycatchlog instead? Is this a feature that is not supported yet?

BR /Edgar

edgBR commented 3 years ago

Well,

It seems that finally I managed to do what I wanted, a small PoC:

library(vroom)
dataImport <- function(input_path, file_name) {
  data_out <- tryCatch(
    vroom(file = paste(input_path, file_name, sep="/")), 
    error = function(e) {e}, 
    warning = function(w) {w}
    )
  if(inherits(data_out, "error")){
    log_error(data_out$message)
  } else if(inherits(data_out, "warning")){
    log_warn(data_out$message)
  } else {
    log_info("Reading {file_name} was successful")
  }
  return(data_out)
}

However it would be nice that a log_error() automatically stops my code.

stPhena commented 3 years ago

Would the use of logger::log_failure(e) be a solution for your case?