appender_file: Append log messages to a file

View source: R/appenders.R

appender_fileR Documentation

Append log messages to a file

Description

Log messages are written to a file with basic log rotation: when max number of lines or bytes is defined to be other than Inf, then the log file is renamed with a .1 suffix and a new log file is created. The renaming happens recursively (eg logfile.1 renamed to logfile.2) until the specified max_files, then the oldest file (logfile.{max_files-1}) is deleted.

Usage

appender_file(
  file,
  append = TRUE,
  max_lines = Inf,
  max_bytes = Inf,
  max_files = 1L
)

Arguments

file

path

append

boolean passed to cat defining if the file should be overwritten with the most recent log message instead of appending

max_lines

numeric specifying the maximum number of lines allowed in a file before rotating

max_bytes

numeric specifying the maximum number of bytes allowed in a file before rotating

max_files

integer specifying the maximum number of files to be used in rotation

Value

function taking lines argument

See Also

Other log_appenders: appender_async(), appender_console(), appender_kinesis(), appender_pushbullet(), appender_slack(), appender_stdout(), appender_syslog(), appender_tee(), appender_telegram()

Examples


## ##########################################################################
## simple example logging to a file
t <- tempfile()
log_appender(appender_file(t))
for (i in 1:25) log_info(i)
readLines(t)

## ##########################################################################
## more complex example of logging to file
## rotated after every 3rd line up to max 5 files

## create a folder storing the log files
t <- tempfile()
dir.create(t)
f <- file.path(t, "log")

## define the file logger with log rotation enabled
log_appender(appender_file(f, max_lines = 3, max_files = 5L))

## enable internal logging to see what's actually happening in the logrotate steps
log_threshold(TRACE, namespace = ".logger")
## log 25 messages
for (i in 1:25) log_info(i)

## see what was logged
lapply(list.files(t, full.names = TRUE), function(t) {
  cat("\n##", t, "\n")
  cat(readLines(t), sep = "\n")
})



daroczig/logger documentation built on Oct. 22, 2024, 12:25 p.m.