Skip to content

Commit

Permalink
add helper functions to automatically log messages/warnings/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Apr 15, 2019
1 parent a953572 commit 111e905
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Date: 2018-12-20
URL: https://daroczig.github.io/logger
BugReports: https://github.com/daroczig/logger/issues
Encoding: UTF-8
RoxygenNote: 6.1.0
RoxygenNote: 6.1.1
License: AGPL-3
Imports:
utils
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ export(layout_simple)
export(log_appender)
export(log_debug)
export(log_error)
export(log_errors)
export(log_eval)
export(log_fatal)
export(log_formatter)
export(log_info)
export(log_layout)
export(log_level)
export(log_messages)
export(log_success)
export(log_threshold)
export(log_trace)
export(log_warn)
export(log_warnings)
export(logger)
export(skip_formatter)
export(with_log_threshold)
Expand Down
52 changes: 52 additions & 0 deletions R/hooks.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' Injects a logger call to standard messages
#'
#' This function uses \code{trace} to add a \code{log_info} function call when \code{message} is called to log the informative messages with the \code{logger} layout and appender.
#' @return
#' @export
#' @examples \dontrun{
#' log_messages()
#' message('hi there')
#' }
log_messages <- function() {
invisible(suppressMessages(trace(
what = 'message',
trace = substitute(logger::log_info(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
}


#' 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.
#' @return
#' @export
#' @examples \dontrun{
#' log_warnings()
#' for (i in 1:5) { Sys.sleep(runif(1)); warning(i) }
#' }
log_warnings <- function() {
invisible(suppressMessages(trace(
what = 'warning',
trace = substitute(logger::log_warn(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
}


#' 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.
#' @return
#' @export
#' @examples \dontrun{
#' log_errors()
#' stop('foobar')
#' }
log_errors <- function() {
invisible(suppressMessages(trace(
what = 'stop',
trace = substitute(logger::log_error(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
}
17 changes: 17 additions & 0 deletions man/log_errors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/log_messages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/log_warnings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 111e905

Please sign in to comment.