Prints a formatted string with optional prefix and end string. The default prefix is a grey timestamp and the default end is a newline character, which is useful for logging messages. All defaults can be set globally using options()
. For details see 'Examples'.
Usage
logf(
fmt,
...,
file = .Options$toscutil.logf.file %||% "",
append = .Options$toscutil.logf.append %||% FALSE,
prefix = .Options$toscutil.logf.prefix %||% function() now_ms(usetz = FALSE, color =
fg$GREY),
sep1 = .Options$toscutil.logf.sep1 %||% " ",
sep2 = .Options$toscutil.logf.sep2 %||% "",
end = .Options$toscutil.logf.end %||% "\n"
)
Arguments
- ...
Arguments to be passed to sprintf for string formatting.
- file
A
file()
connection object or a string naming the file to print to. If""
(the default), output is printed to STDOUT unless redirected bysink()
.- append
Logical. If
TRUE
, the output is appended to the file. IfFALSE
, the file is overwritten.- prefix
A function returning a string to be used as the prefix.
- sep1
A string to be used as the separator between the prefix and the formatted string.
- sep2
A string to be used as the separator between the formatted string and the end.
- end
A string to be used as the end of the message.
Examples
logf("Hello, %s!", "world")
#> 2024-07-11 13:42:50.44 Hello, world!
logf("Goodbye", prefix = function() "", sep1 = "", end = "!\n")
#> Goodbye!
local({
opts <- options(toscutil.logf.prefix = function() "LOG:", toscutil.logf.end = "\n")
on.exit(options(opts))
logf("Hello, %s!", "world")
})
#> LOG: Hello, world!