This function executes immediately before the function definitions for the base handler functions (message, warning, and stop, and logs their timestamped output (a bit more verbosely) to a log file. The log file is an ndjson file, which is a portable, JSON-based format that is easily parsed by many line-processing systems.
loggit(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE, sanitizer)
Level of log output. In actual practice, one of "DEBUG",
"INFO", "WARN", and "ERROR" are common, but any string may be supplied if
custom_log_lvl
is TRUE. Will be coerced to class character
.
Main log message. Will be coerced to class character
.
A named list
or named vector
(each element of length one) of
other custom fields you wish to log. You do not need to explicitly provide
these fields as a formal list or vector, as shown in the example; R handles
the coercion.
Should the log file entry be printed to the console as well?
Defaults to TRUE
, and will print out the ndjson
line to be logged. This
argument is passed as FALSE
when called from loggit
's handlers, since
they still call base R's handlers at the end of execution, all of which
print to the console as well.
Allow log levels other than "DEBUG", "INFO", "WARN",
and "ERROR"? Defaults to FALSE
, to prevent possible typos by the
developer, and to limit the variation in structured log contents. Overall,
setting this to `TRUE`` is not recommended, but is an option for
consistency with other frameworks the user may work with.
Sanitizer function to run over elements in log data. The
default sanitizer, if not specified, is default_ndjson_sanitizer()
. See
the sanitizers documentation for information on how to write your own
(un)sanitizer functions.
# NOT RUN {
loggit("INFO", "This is a message", but_maybe = "you want more fields?",
sure = "why not?", like = 2, or = 10, what = "ever")
# }
Run the code above in your browser using DataLab