daroczig/logger

Feature Request: Capture output from other function as log messages

pwwang opened this issue · 4 comments

pwwang commented
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

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

Yes, that's what I wanted.

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

pwwang commented

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

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.