Creates Logger object which can be used for logging with different level of
verbosity. Log messages are in JSON format.
Methods
Method new()
Creates Logger object.
Usage
Logger$new(
level = c("info", "fatal", "error", "warn", "debug", "trace", "off", "all"),
name = "ROOT",
printer = NULL
)
Arguments
level
Log level. Allowed values: info, fatal, error, warn, debug,
trace, off, all.
name
Logger name.
printer
Logger with sink defined by printer
function.
It should have signature function(timestamp, level, logger_name, pid, message)
.
By default when printer = NULL
logger writes message in JSON format to stdout
.
Method set_name()
Sets logger name.
Usage
Logger$set_name(name = "ROOT")
Method set_log_level()
Sets log level.
Usage
Logger$set_log_level(
level = c("info", "fatal", "error", "warn", "debug", "trace", "off", "all")
)
Arguments
level
Log level. Allowed values: info, fatal, error, warn, debug,
trace, off, all.
Method set_printer()
Sets function which defines how to print logs.
Usage
Logger$set_printer(FUN = NULL)
Arguments
FUN
Printer function. Should be a function with 6 formal arguments:
timestamp, level, logger_name, pid, message.
Write trace message.
Arguments
msg
Log message.
...
Additionals params.
Write debug message.
Arguments
msg
Log message.
...
Additionals params.
Method info()
Write information message.
Arguments
msg
Log message.
...
Additionals params.
Method warn()
Write warning message.
Arguments
msg
Log message.
...
Additionals params.
Method error()
Write error message.
Arguments
msg
Log message.
...
Additionals params.
Method fatal()
Write fatal error message.
Arguments
msg
Log message.
...
Additionals params.
Method clone()
The objects of this class are cloneable with this method.
Usage
Logger$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
# init logger
logger = Logger$new("info")
# write info message
logger$info("hello world")
#> {"timestamp":"2024-04-18 01:55:05.799259","level":"INFO","name":"ROOT","pid":3468,"msg":"hello world"}
# write extended log entry
logger$info("", context = list(message = "hello world", code = 0L))
#> {"timestamp":"2024-04-18 01:55:05.801573","level":"INFO","name":"ROOT","pid":3468,"msg":"","context":{"message":"hello world","code":0}}