logger icon indicating copy to clipboard operation
logger copied to clipboard

Feature Request: Capture output from other function as log messages

Open pwwang opened this issue 2 years ago • 4 comments


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

pwwang avatar Nov 18 '23 05:11 pwwang

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.

daroczig avatar Nov 18 '23 21:11 daroczig

Yes, that's what I wanted.

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

pwwang avatar Nov 18 '23 23:11 pwwang

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

pwwang avatar Nov 27 '23 16:11 pwwang

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.

daroczig avatar Feb 29 '24 20:02 daroczig