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

Utility function to suppress log messages #78

Closed tdeenes closed 6 months ago

tdeenes commented 3 years ago

Especially in unit tests it would be helpful to be able to suppress log messages. Something like:

#' Temporarily suppress log messages
#' 
#' A wrapper around the more powerful \code{\link{with_log_threshold}} which suppresses any logger log messages.
#' @param expr the expression to evaluate
#' @param ... arguments passed to \code{\link[logger]{with_log_threshold}}
#' @export
suppress_log <- function(expr, ...) {
  lvl <- logger::FATAL
  lvl[] <- lvl - 1L
  eval.parent(logger::with_log_threshold(expr, threshold = lvl, ...))
}

Would you be interested in this feature? The code above is a bit hackish since I introduce a non-supported log level which might break if you change the internals.

daroczig commented 3 years ago

Can you share a bit more about the use case? Maybe it's easier and less of a hack to change the appender to write to /dev/null or something like that (eg creating appender_void).

1beb commented 2 years ago

Are you not just asking to set the log level? See ?log_threshold if you set the threshold to WARN, you wouldn't see any level below it: TRACE, DEBUG, INFO, SUCCESS would no longer be shown. Only WARN and above.

FATAL severe error that will prevent the application from continuing
ERROR An error in the application, possibly recoverable
WARN An event that might possible lead to an error
SUCCESS An explicit success event above the INFO level that you want to log
INFO An event for informational purposes
DEBUG A general debugging event
TRACE A fine-grained debug message, typically capturing the flow through the application.

You could set the log_threshold in your test file(s).

tdeenes commented 2 years ago

Sorry for the substantial delay with my response. @1beb I am well aware of log_threshold, see the code snippet in the original comment. @daroczig Indeed, your suggestion with appender_void is a good idea. However, it still requires to loop through the logging namespaces and set the appender, then revert those changes on exit.

My use case is similar to those of suppressWarnings or suppressPackageStartupMessages. e.g.:

So suppress_log would be a convenience utility which would temporarily suppress all messages generated by any logger namespaces when evaluating an expression. Basically not a must-have feature, I agree, so feel free to close the issue.

daroczig commented 6 months ago

Sorry for not getting back on this for so long :disappointed:

I am not even sure since when, but now we have with_log_threshold and also a special log level for not logging anything at all via OFF, so that two make up appender_void if I get this right, and can be useful in cases that you have described.

Closing, but pls reopen if I got this wrong.

tdeenes commented 6 months ago

Oh yeah, I wanted to close this ticket when I saw the new OFF log level, but forgot to do it. Thanks!