From 64dfc72d9b889921bc96b5ec36f85d9d6d7c9d65 Mon Sep 17 00:00:00 2001 From: Ashish Magadum Date: Mon, 9 Nov 2026 17:11:38 -0600 Subject: [PATCH] fix log file append logic --- R/utils_logging.R | 6 ++++-- inst/tinytest/test_logging.R | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/R/utils_logging.R b/R/utils_logging.R index 4529a70e8..147c462c5 100644 --- a/R/utils_logging.R +++ b/R/utils_logging.R @@ -28,10 +28,12 @@ #' @keywords internal .fileAppender = function(log_file_path, append = TRUE) { force(log_file_path) - force(append) + if (!append) { + file.create(log_file_path) # clears the file/creates new if append = FALSE + } function(level, ...) { cat(.formatLogMessage(level, ...), file = log_file_path, sep = "", - append = append) + append = TRUE) } } diff --git a/inst/tinytest/test_logging.R b/inst/tinytest/test_logging.R index 368d181a3..90bad659c 100644 --- a/inst/tinytest/test_logging.R +++ b/inst/tinytest/test_logging.R @@ -5,3 +5,22 @@ expect_null( # For .onLoad expect_true(!is.null(getOption("MSstatsLog"))) expect_true(!is.null(getOption("MSstatsMsg"))) + +old_opts = options("MSstatsLog", "MSstatsMsg") + +f = tempfile(fileext = ".log") +writeLines("old line", f) +MSstatsConvert::MSstatsLogsSettings(TRUE, append = FALSE, verbose = FALSE, + log_file_path = f) +getOption("MSstatsLog")("INFO", "first") +getOption("MSstatsLog")("INFO", "second") +lines = readLines(f) +expect_equal(length(lines), 2L) +expect_false(any(grepl("old line", lines))) + +MSstatsConvert::MSstatsLogsSettings(TRUE, append = TRUE, verbose = FALSE, + log_file_path = f) +getOption("MSstatsLog")("INFO", "third") +expect_equal(length(readLines(f)), 3L) + +options(old_opts)