A simple logging utility that writes the log message to STDOUT or STDERR in plain text, JSON, or comma separated format.
Arguments
- title
Character, the title of the logging message.
- message
Character, a more detailed logging message.
- level
Log level; one of
"ALL"
,"TRACE"
,"DEBUG"
,"INFO"
,"SUCCESS"
,"WARN"
,"ERROR"
,"FATAL"
,"OFF"
(case insensitive).- format
Log format:
"PLAIN"
(default),"JSON"
,"CSV"
(case insensitive).- digits
Integer of length 1, digits for seconds (default
3L
meaning milliseconds).- x, y
Strings to combine.
Value
msg
invisibly returns logical indicating if the log message was written
(TRUE
) or not (FALSE
). A side effect is a log message to STDOUT or STDERR.
The %+%
special pastes the right and left hand side together into s single string.
Details
The TRYR_ERR_LEVEL
environment variable determines where the log message is written.
If the log level is at least the one or above the TRYR_ERR_LEVEL
value
(or "WARN"
when it is unset or null) the message is written to STDERR,
otherwise it is written to STDOUT.
The log message is only written when the log level is at least the one or higher than specified
by the TRYR_LOG_LEVEL
environment variables; or "INFO"
when the variable is unset or null.
The log format can be plain text, JSON, or comma separated text.
When the log format is NULL
the TRYR_LOG_FORMAT
environment variables is checked.
If it is unset or null, the format is considered plain text.
The logging message will be formed by combining the title
and the message
parts.
The log info also contains the process ID, a timestamp (using Sys.time()
), and the log level.
The timestamp prints out fractional seconds according to digits
. When digits
is NULL
it checks the TRYR_LOG_DIGITS
environment variables and uses that value.
The default is 3 when TRYR_LOG_DIGITS
unset or null.
Besides the usual log levels, there is an extra one "SUCCESS"
that is
used to signal successful HTTP response codes (2xx).
Examples
n <- 5
"Sample " %+% "size " %+% "n = " %+% n %+% "."
#> [1] "Sample size n = 5."
msg("Success", "We did it!")
#> 7382 | 2024-05-30 07:19:39.877 [INFO ] Success - We did it!
msg("Success", "We did it!", "SUCCESS")
#> 7382 | 2024-05-30 07:19:39.878 [SUCCESS] Success - We did it!
msg("Error", "Oh no! n cannot be " %+% n, "ERROR")
msg("Success", "We did it!", "SUCCESS", format = "JSON")
#> {"pid":"7382","ts":"2024-05-30 07:19:39.882","ut":1717053579.88199,"level":"SUCCESS","value":4,"title":"Success","message":"We did it!"}
msg("Success", "We did it!", format = "JSON")
#> {"pid":"7382","ts":"2024-05-30 07:19:39.882","ut":1717053579.88288,"level":"INFO","value":3,"title":"Success","message":"We did it!"}
msg("Error", "Oh no ...", "ERROR", format = "JSON")
msg("Success", "We did it!", digits = 0)
#> 7382 | 2024-05-30 07:19:39 [INFO ] Success - We did it!
msg("Success", "We did it!", digits = 6)
#> 7382 | 2024-05-30 07:19:39.887841 [INFO ] Success - We did it!