| Title: | Lightweight Logging for R Scripts |
|---|---|
| Description: | Provides flexible but lightweight logging facilities for R scripts. Supports priority levels for logs and messages, flagging messages, capturing script output, switching logs, and logging to files or connections. |
| Authors: | Ben Bond-Lamberty [cre, aut] (ORCID: <https://orcid.org/0000-0001-9525-4633>) |
| Maintainer: | Ben Bond-Lamberty <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.1 |
| Built: | 2026-05-18 08:25:05 UTC |
| Source: | https://github.com/bpbond/luzlogr |
Close current logfile
closelog(sessionInfo = TRUE)closelog(sessionInfo = TRUE)
sessionInfo |
Append |
Close current logfile. The number of flagged messages is returned,
invisibly. Note that if options(luzlogr.close_on_error = TRUE) is set, then
if an error occurs, all log files will be automatically closed. This behavior
is not currently enabled by default.
Logs are stored on a stack, and so when one is closed, logging output returns to the previous log (if any).
Number of flagged messages (numeric).
If the log was being written to a connection,
closelog will return the connection to its pre-logging state,
whether open or closed.
logfile1 <- openlog("A.log") printlog("message to A", flag = TRUE) logfile2 <- openlog("B.log") printlog("message to B") flagcountB <- closelog() flagcountA <- closelog(sessionInfo = FALSE) file.remove(logfile1, logfile2)logfile1 <- openlog("A.log") printlog("message to A", flag = TRUE) logfile2 <- openlog("B.log") printlog("message to B") flagcountB <- closelog() flagcountA <- closelog(sessionInfo = FALSE) file.remove(logfile1, logfile2)
Open a new logfile
openlog(file, loglevel = -Inf, append = FALSE, sink = FALSE)openlog(file, loglevel = -Inf, append = FALSE, sink = FALSE)
file |
Name of logfile (character or writeable |
loglevel |
Minimum priority level (numeric, optional) |
append |
Append to logfile? (logical, optional) |
sink |
Send all console output to logfile? (logical, optional) |
Open a new logfile. Messages will only appear in the logfile
if their level exceeds the log's loglevel;
this allows you to easily change the amount of detail being logged.
Re-opening a logfile will erase the previous output unless append
is TRUE. Opening a new logfile when one is already open will temporarily
switch logging to that new file.
If sink is TRUE, all screen output will be captured (via sink).
Invisible fully-qualified name of log file.
logfile <- openlog("test.log") printlog("message") closelog() readLines(logfile) file.remove(logfile)logfile <- openlog("test.log") printlog("message") closelog() readLines(logfile) file.remove(logfile)
Log a message
printlog(..., level = 0, ts = TRUE, cr = TRUE, flag = FALSE, flush = FALSE) flaglog(...)printlog(..., level = 0, ts = TRUE, cr = TRUE, flag = FALSE, flush = FALSE) flaglog(...)
... |
Expressions to be printed to the log |
level |
Priority level (numeric, optional) |
ts |
Print preceding timestamp? (logical, optional) |
cr |
Print trailing newline? (logical, optional) |
flag |
Flag this message (e.g. error or warning) (logical, optional) |
flush |
Immediately flush output to file (logical, optional) |
Logs a message, which consists of zero or more printable objects. Simple objects (numeric and character) are printed together on a single line, whereas complex objects (data frames, etc) start on a new line by themselves.
If the current log was opened with sink = TRUE,
messages are printed to the screen, otherwise not. Messages can be flagged;
flaglog assumes
that the message is to be flagged, whereas printlog does not.
Messages will only appear in the logfile if their level exceeds
the log's loglevel; this allows you to easily change the amount of
detail being logged.
Invisible success (TRUE) or failure (FALSE).
A message's preceding timestamp and following carriage return can be
suppressed using the ts and cr parameters.
logfile <- openlog("test.log") printlog("message") printlog(1, "plus", 1, "equals", 1 + 1) closelog() readLines(logfile) file.remove(logfile) logfile <- openlog("test", loglevel = 1) printlog("This message will not appear", level = 0) printlog("This message will appear", level = 1) closelog() readLines(logfile) file.remove(logfile)logfile <- openlog("test.log") printlog("message") printlog(1, "plus", 1, "equals", 1 + 1) closelog() readLines(logfile) file.remove(logfile) logfile <- openlog("test", loglevel = 1) printlog("This message will not appear", level = 0) printlog("This message will appear", level = 1) closelog() readLines(logfile) file.remove(logfile)