Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions R/configurations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 `NULL`, see `rotate_logs()` for implementation.
#' @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 = NULL, 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
}
5 changes: 5 additions & 0 deletions R/loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
11 changes: 7 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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[(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)
}
}
9 changes: 9 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions man/get_rotate_lines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/set_rotate_lines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions tests/testthat/test_loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,31 @@ 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 <- 10

set_rotate_lines(rotate_lines) # turn on
for (i in 1:(rotate_lines + 10)) {
loggit("INFO", paste0("log_", i), echo = FALSE)
log_df <- read_logs()
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:(rotate_lines + 10)) {
loggit("INFO", paste0("log_", i), echo = FALSE)
log_df <- read_logs()
expect_true(nrow(log_df) == i + rotate_lines)
}

})
cleanup()