daroczig/logger

log_formater disables layout_glue_generator

camult opened this issue · 2 comments

Is there a way to use the layout_glue_generator and formatter_pander?

library("logger")

logfile <- "log.txt"
FMLN <- "Something"

logger::log_formatter(formatter_pander)
logLayout <- "{node} \t {user} \t {pid} \t {time} \t {fn} \t {FMLN} \t {level} \t {msg}\n"
logger <- logger::layout_glue_generator(format = logLayout)
logger::log_layout(logger)
logger::log_threshold(TRACE)
RULE  <<- structure(510L, level = "RULE",  class = c("loglevel", "integer"))
TIME  <<- structure(520L, level = "TIME",  class = c("loglevel", "integer"))
COUNT <<- structure(530L, level = "COUNT", class = c("loglevel", "integer"))
logger::log_appender(logger::appender_tee(logfile))
logger::log_level(TIME, "Program Started on {format(Sys.time(), '%b %d %Y at %I:%M %p')}")
logger::log_info("Using {as.character(R.Version()$version.string)}")
logger::log_info(data.frame(a = 1:3, b = 4:6))

Results

server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 TIME 	 Program Started on Mar 28 2024 at 01:06 PM
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 Using R version 4.2.1 (2022-06-23)
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 -------
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	  a   b 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 --- ---
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	  1   4 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	  2   5 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	  3   6 
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 -------
server 	 brito 	 2926921 	 2024-03-28 13:06:16 	 NA 	 Rules Engine 	 INFO 	 
server 	 brito 	 2926921 	 2024-03-28 13:06:33 	 NA 	 Rules Engine 	 TIME 	 Program Started on {format(Sys.time(), '%b %d %Y at %I:%M %p')}

Suggestion...

formatter_glue_pander <- structure(function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) {
  if (any(class(...) %in% c("data.table", "data.frame", "matrix"))) {
    fail_on_missing_package('pander')
    eval(pander::pander_return(...), envir = .topenv)
  } else {
    fail_on_missing_package('glue')
    as.character(
      tryCatch(
        glue::glue(..., .envir = .topenv),
        error = function(e) {
          stop(paste(
            '`glue_safe` failed in `formatter_glue_safe` on:\n\n',
            capture.output(str(...)),
            '\n\nRaw error message:\n\n',
            e$message,
            '\n\nPlease consider using another `log_formatter` or',
            '`skip_formatter` on strings with curly braces.'))
        }))
  }
}, generator = quote(formatter_glue_pander()))