From 909c0cdcd685a6cebd2d6dd50d5a664f02861a55 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 11 Apr 2022 13:30:50 -0600 Subject: [PATCH 01/10] Avoid negative row indices This can happen when rotate_lines > nrow(log_df) Signed-off-by: Peter Solymos --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 1293e98..72317be 100644 --- a/R/utils.R +++ b/R/utils.R @@ -82,6 +82,6 @@ read_logs <- function(logfile, unsanitizer) { rotate_logs <- function(rotate_lines = 100000, logfile) { if (missing(logfile)) logfile <- get_logfile() log_df <- read_logs(logfile) - log_df <- log_df[(nrow(log_df) - rotate_lines + 1):nrow(log_df), ] + log_df <- log_df[max(1L, (nrow(log_df) - rotate_lines + 1)):nrow(log_df), ] write_ndjson(log_df, logfile, echo = FALSE, overwrite = TRUE) } From 026d1787dad10200f3602e45cb617eed04a914a7 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 11 Apr 2022 13:31:36 -0600 Subject: [PATCH 02/10] Add config for log rotation Signed-off-by: Peter Solymos --- R/configurations.R | 43 +++++++++++++++++++++++++++++++++++++++++++ R/zzz.R | 9 +++++++++ 2 files changed, 52 insertions(+) diff --git a/R/configurations.R b/R/configurations.R index 3c1ec29..6cad9b4 100644 --- a/R/configurations.R +++ b/R/configurations.R @@ -81,3 +81,46 @@ set_timestamp_format <- function(ts_format = "%Y-%m-%dT%H:%M:%S%z", confirm = TR get_timestamp_format <- function() { .config$ts_format } + +#' Set Log File Rotation +#' +#' Set configuration for the number of lines after which log files are. +#' truncated. The function will echo out the currently set line number if +#' is set, or else it will echo that there is no limit set. +#' +#' This function provides a means of setting automatic log file truncation. +#' Log files are not truncated when the value is set to `NULL`, which is +#' also the default setting. +#' +#' @param rotate_lines The number of log entries to keep in the logfile. +#' Defaults to 100,000, see `rotate_logs()`. +#' @param confirm Print confirmation message of timestamp format? Defaults to +#' `TRUE`. +#' +#' @examples +#' set_rotate_lines(100) # set limit to 100 lines +#' set_rotate_lines(NULL) # turn off auto-truncation +#' +#' @export +set_rotate_lines <- function(rotate_lines = 100000, confirm = TRUE) { + .config$rotate_lines <- rotate_lines + if (confirm) { + if (is.null(rotate_lines)) { + print(paste0("Log file is not auto-truncated")) + } else { + print(paste0("Truncate log file after ", rotate_lines, " lines")) + } + } +} + +#' Get Log File Rotation +#' +#' Get the configuration for the number of lines after which log files are +#' truncated. +#' +#' @examples get_rotate_lines() +#' +#' @export +get_rotate_lines <- function() { + .config$rotate_lines +} diff --git a/R/zzz.R b/R/zzz.R index 0a83d26..a93df9a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,4 +1,13 @@ .onLoad <- function(libname, pkgname) { set_logfile(confirm = FALSE) set_timestamp_format(confirm = FALSE) + set_rotate_lines(NULL, confirm = FALSE) + invisible(NULL) +} + +.onUnload <- function(libpath) { + for (i in names(.config)) { + .config[[i]] <- NULL + } + invisible(NULL) } From 30de1bdc8674b5fb1f977d0f1652ab2bbbe02fc3 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 11 Apr 2022 13:31:57 -0600 Subject: [PATCH 03/10] Document log rotation configs Signed-off-by: Peter Solymos --- NAMESPACE | 2 ++ man/get_rotate_lines.Rd | 16 ++++++++++++++++ man/set_rotate_lines.Rd | 30 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 man/get_rotate_lines.Rd create mode 100644 man/set_rotate_lines.Rd diff --git a/NAMESPACE b/NAMESPACE index 03cf87f..c1c706d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,14 @@ # Generated by roxygen2: do not edit by hand export(get_logfile) +export(get_rotate_lines) export(get_timestamp_format) export(loggit) export(message) export(read_logs) export(rotate_logs) export(set_logfile) +export(set_rotate_lines) export(set_timestamp_format) export(stop) export(warning) diff --git a/man/get_rotate_lines.Rd b/man/get_rotate_lines.Rd new file mode 100644 index 0000000..d444b59 --- /dev/null +++ b/man/get_rotate_lines.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/configurations.R +\name{get_rotate_lines} +\alias{get_rotate_lines} +\title{Get Log File Rotation} +\usage{ +get_rotate_lines() +} +\description{ +Get the configuration for the number of lines after which log files are +truncated. +} +\examples{ +get_rotate_lines() + +} diff --git a/man/set_rotate_lines.Rd b/man/set_rotate_lines.Rd new file mode 100644 index 0000000..c0c8630 --- /dev/null +++ b/man/set_rotate_lines.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/configurations.R +\name{set_rotate_lines} +\alias{set_rotate_lines} +\title{Set Log File Rotation} +\usage{ +set_rotate_lines(rotate_lines = 1e+05, confirm = TRUE) +} +\arguments{ +\item{rotate_lines}{The number of log entries to keep in the logfile. +Defaults to 100,000, see \code{rotate_logs()}.} + +\item{confirm}{Print confirmation message of timestamp format? Defaults to +\code{TRUE}.} +} +\description{ +Set configuration for the number of lines after which log files are. +truncated. The function will echo out the currently set line number if +is set, or else it will echo that there is no limit set. +} +\details{ +This function provides a means of setting automatic log file truncation. +Log files are not truncated when the value is set to \code{NULL}, which is +also the default setting. +} +\examples{ +set_rotate_lines(100) # set limit to 100 lines +set_rotate_lines(NULL) # turn off auto-truncation + +} From 13b0842021bdc44fa16a90d1bf14c757a5378543 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 11 Apr 2022 13:32:15 -0600 Subject: [PATCH 04/10] Add auto log rotation functionality Signed-off-by: Peter Solymos --- R/loggit.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/loggit.R b/R/loggit.R index 2339f58..71ce740 100644 --- a/R/loggit.R +++ b/R/loggit.R @@ -81,4 +81,9 @@ loggit <- function(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE, s } write_ndjson(log_df, echo = echo) + + # Truncate log files if needed + if (!is.null(get_rotate_lines())) { + rotate_logs(get_rotate_lines(), get_logfile()) + } } From 51d79cb4b2e5785baa96deb1889991d211ee5510 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 11 Apr 2022 13:32:30 -0600 Subject: [PATCH 05/10] Bunp roxygen version Signed-off-by: Peter Solymos --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2bfd6b4..38fbb33 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,7 @@ Suggests: testthat (>= 2.0.0) URL: https://github.com/ryapric/loggit BugReports: https://github.com/ryapric/loggit/issues -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 Encoding: UTF-8 Roxygen: list(markdown = TRUE) VignetteBuilder: knitr From 1467c290b0b9c69f6cac30c7e5f6e0c9ea6e84cb Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Tue, 12 Apr 2022 17:05:08 -0600 Subject: [PATCH 06/10] Make NULL the default in set_rotate_lines --- R/configurations.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/configurations.R b/R/configurations.R index 6cad9b4..99a2a91 100644 --- a/R/configurations.R +++ b/R/configurations.R @@ -93,7 +93,7 @@ get_timestamp_format <- function() { #' also the default setting. #' #' @param rotate_lines The number of log entries to keep in the logfile. -#' Defaults to 100,000, see `rotate_logs()`. +#' Defaults to `NULL`, see `rotate_logs()` for implementation. #' @param confirm Print confirmation message of timestamp format? Defaults to #' `TRUE`. #' @@ -102,7 +102,7 @@ get_timestamp_format <- function() { #' set_rotate_lines(NULL) # turn off auto-truncation #' #' @export -set_rotate_lines <- function(rotate_lines = 100000, confirm = TRUE) { +set_rotate_lines <- function(rotate_lines = NULL, confirm = TRUE) { .config$rotate_lines <- rotate_lines if (confirm) { if (is.null(rotate_lines)) { From 5bdd6dfcf9e322000357c02a9672d6553d48dcf3 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Tue, 12 Apr 2022 17:05:32 -0600 Subject: [PATCH 07/10] Update set_rotate_lines.Rd --- man/set_rotate_lines.Rd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/man/set_rotate_lines.Rd b/man/set_rotate_lines.Rd index c0c8630..f664b54 100644 --- a/man/set_rotate_lines.Rd +++ b/man/set_rotate_lines.Rd @@ -4,11 +4,11 @@ \alias{set_rotate_lines} \title{Set Log File Rotation} \usage{ -set_rotate_lines(rotate_lines = 1e+05, confirm = TRUE) +set_rotate_lines(rotate_lines = NULL, confirm = TRUE) } \arguments{ \item{rotate_lines}{The number of log entries to keep in the logfile. -Defaults to 100,000, see \code{rotate_logs()}.} +Defaults to \code{NULL}, see \code{rotate_logs()} for implementation.} \item{confirm}{Print confirmation message of timestamp format? Defaults to \code{TRUE}.} From bd058c288f26d40b6c54349b35ac7a0a9f265bc0 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Tue, 12 Apr 2022 17:05:58 -0600 Subject: [PATCH 08/10] rotate_logs can accept NULL --- R/utils.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index 72317be..f71323f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -80,8 +80,11 @@ read_logs <- function(logfile, unsanitizer) { #' #' @export rotate_logs <- function(rotate_lines = 100000, logfile) { - if (missing(logfile)) logfile <- get_logfile() - log_df <- read_logs(logfile) - log_df <- log_df[max(1L, (nrow(log_df) - rotate_lines + 1)):nrow(log_df), ] - write_ndjson(log_df, logfile, echo = FALSE, overwrite = TRUE) + if (!is.null(rotate_lines)) { + rotate_lines <- as.integer(rotate_lines) + if (missing(logfile)) logfile <- get_logfile() + log_df <- read_logs(logfile) + log_df <- log_df[max(1L, (nrow(log_df) - rotate_lines + 1)):nrow(log_df), ] + write_ndjson(log_df, logfile, echo = FALSE, overwrite = TRUE) + } } From 93830b5ccedc890117aa65b82529fef75d49bf26 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Tue, 12 Apr 2022 18:45:52 -0600 Subject: [PATCH 09/10] Add test for auto log rotation --- tests/testthat/test_loggit.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/testthat/test_loggit.R b/tests/testthat/test_loggit.R index e89a98f..6fd40a2 100644 --- a/tests/testthat/test_loggit.R +++ b/tests/testthat/test_loggit.R @@ -39,3 +39,27 @@ test_that("Log file is returned via read_logs()", { expect_true("data.frame" %in% class(log_df)) }) cleanup() + + +# == + +test_that("Automatic log rotation is working", { + + rotate_lines <- 50 + + set_rotate_lines(rotate_lines) # turn on + for (i in 1:100) { + loggit("INFO", paste0("log_", i), echo = FALSE) + log_df <- read_logs() + expect_true(nrow(log_df) <= rotate_lines) + } + + set_rotate_lines(NULL) # turn off + for (i in 1:100) { + loggit("INFO", paste0("log_", i), echo = FALSE) + log_df <- read_logs() + expect_true(nrow(log_df) == i + rotate_lines) + } + +}) +cleanup() From 93318adea3de6c331a0e66c94883c85a24243e83 Mon Sep 17 00:00:00 2001 From: Peter Solymos Date: Mon, 18 Apr 2022 18:53:47 -0600 Subject: [PATCH 10/10] Make test robust to the rotation cutoff Re #21 --- tests/testthat/test_loggit.R | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test_loggit.R b/tests/testthat/test_loggit.R index 6fd40a2..6255137 100644 --- a/tests/testthat/test_loggit.R +++ b/tests/testthat/test_loggit.R @@ -45,17 +45,21 @@ cleanup() test_that("Automatic log rotation is working", { - rotate_lines <- 50 + rotate_lines <- 10 set_rotate_lines(rotate_lines) # turn on - for (i in 1:100) { + for (i in 1:(rotate_lines + 10)) { loggit("INFO", paste0("log_", i), echo = FALSE) log_df <- read_logs() - expect_true(nrow(log_df) <= rotate_lines) + if (i <= rotate_lines) { + expect_true(nrow(log_df) == i) + } else { + expect_true(nrow(log_df) == rotate_lines) + } } set_rotate_lines(NULL) # turn off - for (i in 1:100) { + for (i in 1:(rotate_lines + 10)) { loggit("INFO", paste0("log_", i), echo = FALSE) log_df <- read_logs() expect_true(nrow(log_df) == i + rotate_lines)