[help] Unable to use logger
when branching
#1202
-
Help
DescriptionI'm experiencing an issue with using the {logger} package in combination with a branching target. The issue does not occur if I remove the branching pattern and restart my session. I have some utility functions defined in a standalone R script that look like this: # R/log-utils.R
`%>%` <- magrittr::`%>%`
set_logger <- function(log_file, log_dir = "./log", overwrite = TRUE) {
if (!fs::dir_exists(log_dir)) {
fs::dir_create(log_dir)
}
name <- log_file %>%
fs::path_file() %>%
fs::path_ext_remove()
log_file <- fs::path(log_dir, glue::glue("{name}.log"))
if (overwrite & fs::file_exists(log_file)) {
message("Overwriting existing log file...")
fs::file_delete(log_file)
}
logger::log_threshold(logger::TRACE)
logger::log_appender(logger::appender_tee(log_file))
return(log_file %>% fs::path_abs() %>% invisible())
}
test_logger <- function(set_log, i) {
logger::log_info("{Sys.time()} this is a log for branch {i}...")
} My main targets file looks like this: #_targets.R
library(targets)
controller <- crew::crew_controller_local(
workers = future::availableCores() - 1,
seconds_idle = 3
)
# Set target options:
tar_option_set(
packages = c("logger"),
format = "qs",
controller = controller,
garbage_collection = TRUE,
memory = "transient",
iteration = "vector"
)
source("R/log-utils.R")
list(
tar_target(
set_log, {
set_logger("_targets.log", overwrite = TRUE)
TRUE
}
),
tar_target(
test_log_directly, {
set_log # Establish dependency
logger::log_info("{Sys.time()} this is a single log!")
}
),
tar_target(
branches,
{
1:10
}
),
tar_target(
test_log_with_branching,
test_logger(set_log, i=branches),
pattern = map(branches)
)
) The above example runs without error, but no logs are actually being written to the > targets::tar_make()
✔ skipped target branches
✔ skipped target set_log
▶ dispatched branch test_log_from_function_f01a3e41
▶ dispatched branch test_log_from_function_09380a2a
▶ dispatched branch test_log_from_function_793701ec
▶ dispatched branch test_log_from_function_64f4179f
▶ dispatched branch test_log_from_function_eeab6b1e
▶ dispatched branch test_log_from_function_e25d26ba
▶ dispatched branch test_log_from_function_42209826
▶ dispatched branch test_log_from_function_9e6cbe62
▶ dispatched branch test_log_from_function_aa995969
▶ dispatched branch test_log_from_function_29a2bc1e
▶ dispatched target test_log_directly
● completed branch test_log_from_function_f01a3e41 [0.021 seconds]
● completed branch test_log_from_function_793701ec [0.02 seconds]
● completed branch test_log_from_function_09380a2a [0.021 seconds]
● completed branch test_log_from_function_aa995969 [0.001 seconds]
● completed target test_log_directly [0.001 seconds]
● completed branch test_log_from_function_eeab6b1e [0.022 seconds]
● completed branch test_log_from_function_e25d26ba [0.019 seconds]
● completed branch test_log_from_function_9e6cbe62 [0.019 seconds]
● completed branch test_log_from_function_42209826 [0.015 seconds]
● completed branch test_log_from_function_64f4179f [0.018 seconds]
● completed branch test_log_from_function_29a2bc1e [0.014 seconds]
● completed pattern test_log_from_function
▶ completed pipeline [3.97 seconds] Logger is working as expected if the #_targets.R
library(targets)
controller <- crew::crew_controller_local(
workers = future::availableCores() - 1,
seconds_idle = 3
)
# Set target options:
tar_option_set(
packages = c("logger"),
format = "qs",
controller = controller,
garbage_collection = TRUE,
memory = "transient",
iteration = "vector"
)
source("R/log-utils.R")
list(
tar_target(
set_log, {
set_logger("_targets.log", overwrite = TRUE)
TRUE
}
),
tar_target(
test_log_directly, {
set_log # Establish dependency
logger::log_info("{Sys.time()} this is a single log!")
}
),
tar_target(
test_log_without_branching,
test_logger(set_log, i=1)
)
) > tar_make()
▶ dispatched target set_log
▶ dispatched target branches
● completed target set_log [0.047 seconds]
▶ dispatched target test_log_without_branching
● completed target branches [0.007 seconds]
▶ dispatched target test_log_directly
● completed target test_log_without_branching [0.002 seconds]
● completed target test_log_directly [0.002 seconds]
▶ completed pipeline [2.576 seconds] Here is what the contents of my log file look like:
A complete reprex including an Any troubleshooting support here is greatly appreciated. Please let me know if you have any questions or need me to help clarify. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
This happens because the |
Beta Was this translation helpful? Give feedback.
This happens because the
crew
worker processes don't know about the settings you specified usinglogger::log_appender()
. If you callset_logger(your_log_file, overwrite = FALSE)
insidetest_logger()
, the log and all the expected lines seem to show up. Since your targets are running in parallel, it is good practice to use separate logs so the processes do not compete for access to a single file. The behavior in those cases is unpredictable: https://www.techtarget.com/searchstorage/definition/race-condition#:~:text=Race%20conditions%20are%20most%20commonly,common%20issue%20for%20multithreaded%20applications.