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.
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
See also
This is generator function for log_appender()
, for
alternatives, see eg appender_console()
, appender_tee()
,
appender_slack()
, appender_pushbullet()
,
appender_telegram()
, appender_syslog()
,
appender_kinesis()
and appender_async()
for evaluate any
log_appender()
function in a background process.
Examples
if (FALSE) { # \dontrun{
## ##########################################################################
## 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))
## 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")
})
## enable internal logging to see what's actually happening in the logrotate steps
log_threshold(TRACE, namespace = ".logger")
## run the above commands again
} # }