Skip to content

Commit

Permalink
refactor: use lgr to print log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Feb 9, 2024
1 parent fa7d2df commit 68cc248
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
3 changes: 2 additions & 1 deletion R/Rush.R
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ Rush = R6::R6Class("Rush",
tab = self$read_log(worker_ids, first_event = private$.log_counter, last_event = -1)
if (nrow(tab)) {
pwalk(tab, function(level, worker_id, logger, timestamp, msg, ...) {
catf("%s [%s] [%s] %s", level, worker_id, timestamp, msg)
pkg_logger = lgr::get_logger(logger)
pkg_logger$log(level, "[%s] [%s] %s", worker_id, timestamp, msg)
})
private$.log_counter = private$.log_counter + nrow(tab)
}
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-Rush.R
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,30 @@ test_that("seed is set correctly on two workers", {

expect_rush_reset(rush, type = "terminate")
})

# log --------------------------------------------------------------------------

test_that("printing logs with redis appender works", {
skip_on_cran()
skip_on_ci()

config = start_flush_redis()
rush = Rush$new(network_id = "test-rush", config = config, seed = 123)
fun = function(x1, x2, ...) {
lg = lgr::get_logger("rush")
lg$info("test-1-info")
lg$warn("test-1-warn")
lg$error("test-1-error")
list(y = x1 + x2)
}
worker_ids = rush$start_workers(fun = fun, n_workers = 1, wait_for_workers = TRUE, lgr_thresholds = c(rush = "info"))
xss = list(list(x1 = 1, x2 = 2))
keys = rush$push_tasks(xss)

Sys.sleep(1)

expect_output(rush$print_log(), ".*test-1-info.*test-1-warn.*test-1-error")
expect_silent(rush$print_log())

expect_rush_reset(rush, type = "terminate")
})
31 changes: 0 additions & 31 deletions tests/testthat/test-RushWorker.R
Original file line number Diff line number Diff line change
Expand Up @@ -898,37 +898,6 @@ test_that("saving logs with redis appender works", {
expect_rush_reset(rush, type = "terminate")
})

test_that("printing logs with redis appender works", {
skip_on_cran()
skip_on_ci()
appenders = lgr::get_logger("root")$appenders

on.exit({
lgr::get_logger("root")$set_appenders(appenders)
})

config = start_flush_redis()
rush = RushWorker$new(
network_id = "test-rush",
config = config,
host = "local",
lgr_thresholds = c(rush = "info"),
lgr_buffer_size = 0)
lg = lgr::get_logger("rush")

lg$info("test-1")

expect_output(rush$print_log(), ".*test-1")
expect_silent(rush$print_log())

lg$info("test-2")
lg$info("test-3")

expect_output(rush$print_log(), ".*test-2.*test-3")

expect_rush_reset(rush, type = "terminate")
})

test_that("settings the buffer size in redis appender works", {
skip_on_cran()
skip_on_ci()
Expand Down

0 comments on commit 68cc248

Please sign in to comment.