old <- logger:::namespaces_set()
## ##########################################################################
## 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")
})
logger:::namespaces_set(old)
Run the code above in your browser using DataLab