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

Add with_log #68

Merged
merged 3 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export(set_timestamp_format)
export(stop)
export(stopifnot)
export(warning)
export(with_loggit)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

## New features
* Add `convert_to_csv()` to convert log files to CSV format.
* Add `with_loggit()` to log third-party code or to easily use different `loggit()`-parameters for a chunk of code.

## Bugfixes
* `read_logs()` now correctly reads empty character values `""`, as in `{"key": ""}`, as such.
Expand Down
5 changes: 3 additions & 2 deletions R/loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' Defaults to `TRUE`.
#' @param custom_log_lvl Allow log levels other than "DEBUG", "INFO", "WARN",
#' and "ERROR"? Defaults to `FALSE`.
#' @inheritParams write_ndjson
#'
#' @return Invisible `NULL`.
#'
Expand All @@ -18,7 +19,7 @@
#' sure = "why not?", like = 2, or = 10, what = "ever")
#'
#' @export
loggit <- function(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE) {
loggit <- function(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE, logfile = get_logfile()) {
# Try to suggest limited log levels to prevent typos by users
log_lvls <- c("DEBUG", "INFO", "WARN", "ERROR")
if (!custom_log_lvl && !(log_lvl %in% log_lvls)) {
Expand Down Expand Up @@ -52,5 +53,5 @@ loggit <- function(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE) {
)
}

write_ndjson(log_df, echo = echo)
write_ndjson(log_df, echo = echo, logfile = logfile)
}
28 changes: 28 additions & 0 deletions R/whit_loggit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Log any expressions
#'
#' Log code without having to explicitly use the `loggit2` handlers.
#' This is particularly useful for code that cannot be customized, e.g. from third-party packages
#'
#' @param exp An expression to evaluate.
#' @inheritParams loggit
#'
#' @details If `loggit2` handlers are already used in the expression, this can lead to conditions being logged
#' twice (in the same or different files)
#'
#' @return The result of the expression.
#'
#' @export
with_loggit <- function(exp, logfile = get_logfile(), echo = TRUE) {
withCallingHandlers(
exp,
error = function(e) {
loggit(log_lvl = "ERROR", log_msg = e$message, echo = echo, logfile = logfile)
},
warning = function(w) {
loggit(log_lvl = "WARN", log_msg = w$message, echo = echo, logfile = logfile)
},
message = function(m) {
loggit(log_lvl = "INFO", log_msg = m$message, echo = echo, logfile = logfile)
}
)
}
12 changes: 11 additions & 1 deletion man/loggit.Rd

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

28 changes: 28 additions & 0 deletions man/with_loggit.Rd

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

79 changes: 79 additions & 0 deletions tests/testthat/test-whit_loggit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
test_that("Message, warning, and error get writen to the log file", {

test_function <- function(x) {
base::message("A message")
base::warning("A warning")
}

expect_warning(
expect_message(
with_loggit(test_function(y), echo = FALSE),
"A message"
),
"A warning"
)

expect_error(
with_loggit({
base::message("A message")
base::stop("An error")
}, echo = FALSE),
"An error"
)

logs_json <- read_logs()
expect_identical(nrow(logs_json), 4L)
expect_identical(logs_json$log_lvl, c("INFO", "WARN", "INFO", "ERROR"))
expect_identical(logs_json$log_msg, c("A message\n", "A warning", "A message\n", "An error"))
})
cleanup()

test_that("Different log files for handler and with_loggit", {

default_logfile <- get_logfile()
with_logfile <- file.path(tempdir(), "with_loggit.log")

test_function <- function(x) {
with_loggit({
base::message("A message")
base::warning("A warning")
}, echo = FALSE, logfile = default_logfile)
}

expect_warning(
expect_message(
with_loggit(test_function(y), echo = FALSE, logfile = with_logfile),
"A message"
),
"A warning"
)

expect_identical(get_logfile(), default_logfile)
default_log <- read_logs(logfile = default_logfile)
with_log <- read_logs(logfile = with_logfile)
expect_identical(default_log$log_lvl, with_log$log_lvl)
expect_identical(default_log$log_msg, with_log$log_msg)

})
cleanup()

test_that("Combination of tryCatch and with_loggit", {
# Message
expect_message(
tryCatch({
with_loggit(base::message("A message"), echo = FALSE)
}, message = function(e) base::message("tryCatch")
),
"tryCatch"
)

# Warning
expect_message(
tryCatch({
with_loggit(base::warning("A message"), echo = FALSE)
}, warning = function(e) base::message("tryCatch")
),
"tryCatch"
)
})
cleanup()
Loading