Title: | Easy-to-Use, Dependencyless Logger |
---|---|
Description: | An easy-to-use 'ndjson' (newline-delimited 'JSON') logger. It provides a set of wrappers for base R's message(), warning(), and stop() functions that maintain identical functionality, but also log the handler message to an 'ndjson' log file. No change in existing code is necessary to use this package, and only a few additional adjustments are needed to fully utilize its potential. |
Authors: | Matthias Ollech [cre, aut], Ryan Price [fnd, aut] |
Maintainer: | Matthias Ollech <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.3.1.9000 |
Built: | 2025-03-09 10:26:00 UTC |
Source: | https://github.com/meo265/loggit2 |
Creates a csv file from the ndjson log file.
convert_to_csv( file, logfile = get_logfile(), unsanitize = FALSE, last_first = FALSE, ... )
convert_to_csv( file, logfile = get_logfile(), unsanitize = FALSE, last_first = FALSE, ... )
file |
Path to write csv file to. |
logfile |
Path to log file to read from. |
unsanitize |
Should escaped special characters be unescaped? |
last_first |
Should the last log entry be the first row of the data frame? |
... |
Additional arguments to pass to |
Unescaping of special characters can lead to unexpected results. Use unsanitize = TRUE
with caution.
Invisible NULL
.
## Not run: convert_to_csv("my_log.csv") convert_to_csv("my_log.csv", logfile = "my_log.log", last_first = TRUE) ## End(Not run)
## Not run: convert_to_csv("my_log.csv") convert_to_csv("my_log.csv", logfile = "my_log.log", last_first = TRUE) ## End(Not run)
Get Call Options
get_call_options()
get_call_options()
The call options.
Get echo
get_echo()
get_echo()
Logical. Are log messages echoed to stdout
?
Return the log file that loggit()
will write to by default.
get_logfile()
get_logfile()
The log file path.
get_logfile()
get_logfile()
Get timestamp format for use in output logs.
get_timestamp_format()
get_timestamp_format()
The timestamp format.
get_timestamp_format()
get_timestamp_format()
Log messages and R objects to a ndjson log file.
loggit( log_lvl, log_msg, ..., echo = get_echo(), custom_log_lvl = FALSE, logfile = get_logfile(), ignore_log_level = FALSE )
loggit( log_lvl, log_msg, ..., echo = get_echo(), custom_log_lvl = FALSE, logfile = get_logfile(), ignore_log_level = FALSE )
log_lvl |
Log level. A atomic vector of length one (usually |
log_msg |
Log message. A atomic vector of length one (usually |
... |
Named arguments, each a atomic vector of length one, you wish to log. Will be coerced to |
echo |
Should the log entry (json) be echoed to |
custom_log_lvl |
Allow log levels other than "DEBUG", "INFO", "WARN", and "ERROR"? |
logfile |
Path of log file to write to. |
ignore_log_level |
Ignore the log level set by |
Invisible NULL
.
## Not run: loggit("DEBUG", "This is a message") loggit("INFO", "This is a message", echo = FALSE) loggit("CUSTOM", "This is a message of a custom log_lvl", custom_log_lvl = TRUE) loggit( "INFO", "This is a message", but_maybe = "you want more fields?", sure = "why not?", like = 2, or = 10, what = "ever" ) ## End(Not run)
## Not run: loggit("DEBUG", "This is a message") loggit("INFO", "This is a message", echo = FALSE) loggit("CUSTOM", "This is a message of a custom log_lvl", custom_log_lvl = TRUE) loggit( "INFO", "This is a message", but_maybe = "you want more fields?", sure = "why not?", like = 2, or = 10, what = "ever" ) ## End(Not run)
This function is identical to base R's message
,
but it includes logging of the exception message via loggit()
.
message(..., domain = NULL, appendLF = TRUE, .loggit = NA, echo = get_echo())
message(..., domain = NULL, appendLF = TRUE, .loggit = NA, echo = get_echo())
... |
zero or more objects which can be coerced to character
(and which are pasted together with no separator) or (for
|
domain |
see |
appendLF |
logical: should messages given as a character string have a newline appended? |
.loggit |
Should the condition message be added to the log?
If |
echo |
Should the log entry (json) be echoed to |
Invisible NULL
.
Other handlers:
stop()
,
stopifnot()
,
warning()
## Not run: message("Don't say such silly things!") message("Don't say such silly things!", appendLF = FALSE, echo = FALSE) ## End(Not run)
## Not run: message("Don't say such silly things!") message("Don't say such silly things!", appendLF = FALSE, echo = FALSE) ## End(Not run)
data.frame
Returns a data.frame
containing all the logs in the provided ndjson
log file.
read_logs(logfile = get_logfile(), unsanitize = TRUE, last_first = FALSE)
read_logs(logfile = get_logfile(), unsanitize = TRUE, last_first = FALSE)
logfile |
Path to log file to read from. |
unsanitize |
Should escaped special characters be unescaped? |
last_first |
Should the last log entry be the first row of the data frame? |
read_logs()
returns a data.frame
with the empty character columns "timestamp", "log_lvl" and "log_msg"
if the log file has no entries.
A data.frame
, with the columns as the fields in the log file.
## Not run: read_logs() read_logs(last_first = TRUE) ## End(Not run)
## Not run: read_logs() read_logs(last_first = TRUE) ## End(Not run)
Truncates the log file to the line count provided as rotate_lines
.
rotate_logs(rotate_lines = 100000L, logfile = get_logfile())
rotate_logs(rotate_lines = 100000L, logfile = get_logfile())
rotate_lines |
The number of log entries to keep in the logfile. |
logfile |
Log file to truncate. |
Invisible NULL
.
## Not run: rotate_logs() rotate_logs(rotate_lines = 0L) rotate_logs(rotate_lines = 1000L, logfile = "my_log.log") ## End(Not run)
## Not run: rotate_logs() rotate_logs(rotate_lines = 0L) rotate_logs(rotate_lines = 1000L, logfile = "my_log.log") ## End(Not run)
Set Call Options
set_call_options(..., .arg_list, confirm = TRUE)
set_call_options(..., .arg_list, confirm = TRUE)
... |
Named arguments to set. |
.arg_list |
A list of named arguments to set. |
confirm |
Print confirmation message of call options setting? |
Call options are as follows:
log_call
: Log the call of an condition?
full_stack
: Log the full stack trace?
Only one of ...
or .arg_list
can be provided.
Invisible the previous call options.
Set echo
set_echo(echo = TRUE, confirm = TRUE)
set_echo(echo = TRUE, confirm = TRUE)
echo |
Should log messages be echoed to |
confirm |
Print confirmation message of echo setting? |
Invisible the previous echo setting.
## Not run: set_echo(TRUE) set_echo(FALSE) ## End(Not run)
## Not run: set_echo(TRUE) set_echo(FALSE) ## End(Not run)
Set Log Level
set_log_level(level = "DEBUG", confirm = TRUE)
set_log_level(level = "DEBUG", confirm = TRUE)
level |
Log level to set, as a string or integer. |
confirm |
Print confirmation message of log level? |
Log levels are as follows: DEBUG: 4 INFO: 3 WARNING: 2 ERROR: 1 NONE: 0
Invisible the previous log level.
## Not run: set_log_level("DEBUG") set_log_level("INFO") set_log_level(4) set_log_level(3) ## End(Not run)
## Not run: set_log_level("DEBUG") set_log_level("INFO") set_log_level(4) set_log_level(3) ## End(Not run)
Set the log file that loggit will write to by default.
set_logfile(logfile = NULL, confirm = TRUE, create = TRUE)
set_logfile(logfile = NULL, confirm = TRUE, create = TRUE)
logfile |
Absolut or relative path to log file.
An attempt is made to convert the path into a canonical absolute form using |
confirm |
Print confirmation of log file setting? |
create |
Create the log file if it does not exist? |
No logs outside of a temporary directory will be written until this is set explicitly, as per CRAN policy.
Therefore, the default behavior is to create a file named loggit.log
in your system's temporary directory.
Invisible the previous log file path.
## Not run: set_logfile("path/to/logfile.log") ## End(Not run)
## Not run: set_logfile("path/to/logfile.log") ## End(Not run)
Set timestamp format for use in output logs.
set_timestamp_format(ts_format = "%Y-%m-%dT%H:%M:%S%z", confirm = TRUE)
set_timestamp_format(ts_format = "%Y-%m-%dT%H:%M:%S%z", confirm = TRUE)
ts_format |
ISO date format. |
confirm |
Print confirmation message of timestamp format? |
This function performs no time format validations, but will echo out the current time in the provided format for manual validation.
This function provides no means of setting a timezone, and instead relies on the host system's time configuration to provide this. This is to enforce consistency across software running on the host.
Invisible the previous timestamp format.
## Not run: set_timestamp_format("%Y-%m-%d %H:%M:%S") ## End(Not run)
## Not run: set_timestamp_format("%Y-%m-%d %H:%M:%S") ## End(Not run)
This function is identical to base R's stop
,
but it includes logging of the exception message via loggit()
.
stop(..., call. = TRUE, domain = NULL, .loggit = NA, echo = get_echo())
stop(..., call. = TRUE, domain = NULL, .loggit = NA, echo = get_echo())
... |
zero or more objects which can be coerced to character (and which are pasted together with no separator) or a single condition object. |
call. |
logical, indicating if the call should become part of the error message. |
domain |
see |
.loggit |
Should the condition message be added to the log?
If |
echo |
Should the log entry (json) be echoed to |
No return value.
Other handlers:
message()
,
stopifnot()
,
warning()
## Not run: stop("This is a completely false condition") stop("This is a completely false condition", echo = FALSE) ## End(Not run)
## Not run: stop("This is a completely false condition") stop("This is a completely false condition", echo = FALSE) ## End(Not run)
This function is identical to base R's stopifnot
,
but it includes logging of the exception message via loggit()
.
stopifnot(..., exprs, exprObject, local, .loggit = NA, echo = get_echo())
stopifnot(..., exprs, exprObject, local, .loggit = NA, echo = get_echo())
... , exprs
|
any number of { expr1 expr2 .... } Note that e.g., positive numbers are not |
exprObject |
alternative to |
local |
(only when |
.loggit |
Should the condition message be added to the log?
If |
echo |
Should the log entry (json) be echoed to |
Other handlers:
message()
,
stop()
,
warning()
## Not run: stopifnot("This is a completely false condition" = FALSE) stopifnot(5L == 5L, "This is a completely false condition" = FALSE, echo = FALSE) ## End(Not run)
## Not run: stopifnot("This is a completely false condition" = FALSE) stopifnot(5L == 5L, "This is a completely false condition" = FALSE, echo = FALSE) ## End(Not run)
This function is identical to base R's warning
,
but it includes logging of the exception message via loggit()
.
warning( ..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, domain = NULL, .loggit = NA, echo = get_echo() )
warning( ..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE, domain = NULL, .loggit = NA, echo = get_echo() )
... |
zero or more objects which can be coerced to character (and which are pasted together with no separator) or a single condition object. |
call. |
logical, indicating if the call should become part of the warning message. |
immediate. |
logical, indicating if the call should be output
immediately, even if |
noBreaks. |
logical, indicating as far as possible the message should
be output as a single line when |
domain |
see |
.loggit |
Should the condition message be added to the log?
If |
echo |
Should the log entry (json) be echoed to |
The warning message as character
string, invisibly.
Other handlers:
message()
,
stop()
,
stopifnot()
## Not run: warning("You may want to review that math") warning("You may want to review that math", immediate = FALSE, echo = FALSE) ## End(Not run)
## Not run: warning("You may want to review that math") warning("You may want to review that math", immediate = FALSE, echo = FALSE) ## End(Not run)
Log code without having to explicitly use the loggit2
handlers.
This is particularly useful for code that cannot be customized, e.g. from third-party packages.
with_loggit( exp, logfile = get_logfile(), echo = get_echo(), log_level = get_log_level() )
with_loggit( exp, logfile = get_logfile(), echo = get_echo(), log_level = get_log_level() )
exp |
An |
logfile |
Path of log file to write to. |
echo |
Should the log entry (json) be echoed to |
log_level |
The log level to use. |
If loggit2
handlers are already used in the expression, this can lead to conditions being logged
twice (in the same or different files).
The result of the expression.
## Not run: x <- with_loggit(5L + 5L) with_loggit(base::message("Test log message")) with_loggit(base::warning("Test log message"), echo = FALSE, logfile = "my_log.log") x <- with_loggit({ y <- 5L base::message("Test log message") base::warning("Test log message") 1L + y }) ## End(Not run)
## Not run: x <- with_loggit(5L + 5L) with_loggit(base::message("Test log message")) with_loggit(base::warning("Test log message"), echo = FALSE, logfile = "my_log.log") x <- with_loggit({ y <- 5L base::message("Test log message") base::warning("Test log message") 1L + y }) ## End(Not run)