Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility function to suppress log messages #78

Closed
tdeenes opened this issue Mar 21, 2021 · 5 comments
Closed

Utility function to suppress log messages #78

tdeenes opened this issue Mar 21, 2021 · 5 comments

Comments

@tdeenes
Copy link
Contributor

tdeenes commented Mar 21, 2021

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
Copy link
Owner

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
Copy link

1beb commented Oct 16, 2021

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
Copy link
Contributor Author

tdeenes commented Nov 8, 2021

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.:

  • You develop pkgC::fooC() which calls pkgB::fooB() which calls pkgA::fooA(). Assume you want to suppress all logger messages within fooC when you call fooB, without bothering with the call chain and other logger namespaces. Then in your tests you want to suppress all logger messages (even those of fooC()) because maybe you know that your unit test will lead fooC to fail.

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
Copy link
Owner

daroczig commented Feb 29, 2024

Sorry for not getting back on this for so long 😞

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.

@daroczig daroczig mentioned this issue Feb 29, 2024
4 tasks
@tdeenes
Copy link
Contributor Author

tdeenes commented Feb 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants