From 68cc248001b7bf9c22fc1cb93f754a9e90137773 Mon Sep 17 00:00:00 2001 From: be-marc Date: Fri, 9 Feb 2024 11:35:01 +0100 Subject: [PATCH] refactor: use lgr to print log messages --- R/Rush.R | 3 ++- tests/testthat/test-Rush.R | 27 +++++++++++++++++++++++++++ tests/testthat/test-RushWorker.R | 31 ------------------------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/R/Rush.R b/R/Rush.R index a834e33..71e9b3d 100644 --- a/R/Rush.R +++ b/R/Rush.R @@ -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) } diff --git a/tests/testthat/test-Rush.R b/tests/testthat/test-Rush.R index 105cccd..1d21778 100644 --- a/tests/testthat/test-Rush.R +++ b/tests/testthat/test-Rush.R @@ -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") +}) diff --git a/tests/testthat/test-RushWorker.R b/tests/testthat/test-RushWorker.R index ca71e55..b0871f3 100644 --- a/tests/testthat/test-RushWorker.R +++ b/tests/testthat/test-RushWorker.R @@ -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()