| Logger {log} | R Documentation |
Logger
Description
Create a logger.
Public fields
printerA callback function to write the message to the console, must accept a single argument, defaults to
cat.predicateA predicate function that determines whether to actually run the log, useful if you want to switch the logger on and off for debugging.
If the function returns
TRUEthe logger runs as normal, ifFALSEthe logger does not actually print, write or dump the messages.
Methods
Public methods
Method new()
Usage
Logger$new(prefix = NULL, write = FALSE, file = "log.log", sep = "\t")
Arguments
prefixString to prefix all log messages.
writeWhether to write the log to the
file.fileName of the file to dump the logs to, only used if
writeisTRUE.sepSeparator between
prefixand other flags and messages.
Details
Initialise
Examples
info <- Logger$new("INFO")
info$log("hello")
Method date()
Usage
Logger$date(format = "%d-%m-%Y")
Arguments
formatFormatter for the item, passed to
format().
Details
Include the date in the log
Examples
info <- Logger$new("INFO")$date()
info$log("today")
Method time()
Usage
Logger$time(format = "%H:%M:%S")
Arguments
formatFormatter for the item, passed to
format().
Details
Include the time in the log
Examples
info <- Logger$new("INFO")$time()
info$log("now")
Method unix()
Usage
Logger$unix()
Details
Include the time in the log
Examples
info <- Logger$new("INFO")$unix()
info$log("timestamp")
Method hook()
Usage
Logger$hook(fn)
Arguments
fnA function that accepts one argument (string) and returns a modified version of that string.
Details
Preprocess the prefix with a custom function
Examples
err <- Logger$new("INFO")$hook(crayon::red)
err$log("hello")
Method dir()
Usage
Logger$dir()
Details
Include the directory in the log
Examples
info <- Logger$new("INFO")$dir()
info$log("directory")
Method flag()
Usage
Logger$flag(what)
Arguments
whatFunction to run for every message logged or string to include in log message.
Details
Pass a custom flag
Examples
fl <- function(){
paste0(sample(letters, 4), collapse = "")
}
info <- Logger$new("INFO")$flag(fl)
info$log("random")
Method log()
Usage
Logger$log(..., sep = " ", collapse = " ")
Arguments
...Elements to compose message.
sep, collapseSeparators passed to
paste().
Details
Log messages
Examples
info <- Logger$new("INFO")
info$log("Logger")
Method dump()
Usage
Logger$dump(file = "dump.log")
Arguments
fileName of the file to dump the logs to.
Details
Dump the log to a file
Examples
info <- Logger$new("INFO")
info$log("hello")
\dontrun{info$dump()}
Method clone()
The objects of this class are cloneable with this method.
Usage
Logger$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
info <- Logger$new("INFO")$
date()$
time()$
hook(crayon::blue)
info$log("Hello")
Sys.sleep(.7)
info$log("World")
## ------------------------------------------------
## Method `Logger$new`
## ------------------------------------------------
info <- Logger$new("INFO")
info$log("hello")
## ------------------------------------------------
## Method `Logger$date`
## ------------------------------------------------
info <- Logger$new("INFO")$date()
info$log("today")
## ------------------------------------------------
## Method `Logger$time`
## ------------------------------------------------
info <- Logger$new("INFO")$time()
info$log("now")
## ------------------------------------------------
## Method `Logger$unix`
## ------------------------------------------------
info <- Logger$new("INFO")$unix()
info$log("timestamp")
## ------------------------------------------------
## Method `Logger$hook`
## ------------------------------------------------
err <- Logger$new("INFO")$hook(crayon::red)
err$log("hello")
## ------------------------------------------------
## Method `Logger$dir`
## ------------------------------------------------
info <- Logger$new("INFO")$dir()
info$log("directory")
## ------------------------------------------------
## Method `Logger$flag`
## ------------------------------------------------
fl <- function(){
paste0(sample(letters, 4), collapse = "")
}
info <- Logger$new("INFO")$flag(fl)
info$log("random")
## ------------------------------------------------
## Method `Logger$log`
## ------------------------------------------------
info <- Logger$new("INFO")
info$log("Logger")
## ------------------------------------------------
## Method `Logger$dump`
## ------------------------------------------------
info <- Logger$new("INFO")
info$log("hello")
## Not run: info$dump()