daroczig / logger

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

Feature Request: Capture output from other function as log messages #129

Open pwwang opened 10 months ago

pwwang commented 10 months ago

func <- function() {
  print("stdout")
}

log_capture(INFO, { func() })

# INFO [2023-11-17 23:34:41] stdout

func <- function() {
  write("stdout", stdout())
  write("stderr", stderr())
}

log_capture(INFO, ERROR, { func() })

# INFO [2023-11-17 23:34:41] stdout
# ERROR [2023-11-17 23:34:41] stderr
daroczig commented 10 months ago

Hm, interesting idea. Just to make sure I understand this: you are looking for a wrapper function that uses sink or similar to capture either/both stdout/stderr and log with provided log level?

Not sure how to avoid the infinite recursion when logging to the console, but otherwise should be doable.

pwwang commented 10 months ago

Yes, that's what I wanted.

Just a naive thought about the recursion: how about using a sentinel variable?

pwwang commented 10 months ago

Or we could check if the inner message follows the logging format?

daroczig commented 7 months ago

Thanks for the feature request! As I am busy with other things, I don't think I have the time to work in this in the coming months, so I would highly appreciate if someone can come up with a PR -- so thus I'm now setting the related "Help wanted" label.