diff --git a/R/hooks.R b/R/hooks.R index aeabfce5..e9d4ef6a 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -37,16 +37,20 @@ log_messages <- function() { #' Injects a logger call to standard warnings #' #' This function uses \code{trace} to add a \code{log_warn} function call when \code{warning} is called to log the warning messages with the \code{logger} layout and appender. +#' @param muffle if TRUE, the warning is not shown after being logged #' @export #' @examples \dontrun{ #' log_warnings() #' for (i in 1:5) { Sys.sleep(runif(1)); warning(i) } #' } -log_warnings <- function() { +log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) { if (R.Version()$major >= 4) { globalCallingHandlers( warning = function(m) { logger::log_warn(m$message) + if (isTRUE(muffle)) { + invokeRestart('muffleWarning') + } } ) } else { @@ -63,16 +67,20 @@ log_warnings <- function() { #' Injects a logger call to standard errors #' #' This function uses \code{trace} to add a \code{log_error} function call when \code{stop} is called to log the error messages with the \code{logger} layout and appender. +#' @param muffle if TRUE, the error is not thrown after being logged #' @export #' @examples \dontrun{ #' log_errors() #' stop('foobar') #' } -log_errors <- function() { +log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) { if (R.Version()$major >= 4) { globalCallingHandlers( error = function(m) { logger::log_error(m$message) + if (isTRUE(muffle)) { + invokeRestart('abort') + } } ) } else { diff --git a/man/log_errors.Rd b/man/log_errors.Rd index 33f3d576..9680ef3e 100644 --- a/man/log_errors.Rd +++ b/man/log_errors.Rd @@ -4,7 +4,10 @@ \alias{log_errors} \title{Injects a logger call to standard errors} \usage{ -log_errors() +log_errors(muffle = getOption("logger_muffle_errors", FALSE)) +} +\arguments{ +\item{muffle}{if TRUE, the error is not thrown after being logged} } \description{ This function uses \code{trace} to add a \code{log_error} function call when \code{stop} is called to log the error messages with the \code{logger} layout and appender. diff --git a/man/log_warnings.Rd b/man/log_warnings.Rd index ce35d0cf..7a0e9895 100644 --- a/man/log_warnings.Rd +++ b/man/log_warnings.Rd @@ -4,7 +4,10 @@ \alias{log_warnings} \title{Injects a logger call to standard warnings} \usage{ -log_warnings() +log_warnings(muffle = getOption("logger_muffle_warnings", FALSE)) +} +\arguments{ +\item{muffle}{if TRUE, the warning is not shown after being logged} } \description{ This function uses \code{trace} to add a \code{log_warn} function call when \code{warning} is called to log the warning messages with the \code{logger} layout and appender.