An R6 class for flexible logging with customisable output, message formatting, and context.
Methods
Method new()
Create a new Logger object.
Arguments
level
The minimum log level to output. Default is LogLevel$INFO.
file_path
Character; the path to a file to save log entries to. Default is NULL.
db_conn
DBI connection object; an existing database connection. Default is NULL.
table_name
Character; the name of the table to log to in the database. Default is "LOGS".
print_fn
Function; custom print function to use for console output. Should accept a single character string as input. Default uses cat with a newline.
format_fn
Function; custom format function to modify the log message. Should accept level and msg as inputs and return a formatted string.
context
List; initial context for the logger. Default is an empty list.
Method set_level()
Set the minimum log level.
Examples
logger <- Logger$new()
logger$set_level(LogLevel$WARNING)
Method error()
Log an error message.
Method warn()
Log a warning message.
Method info()
Log an info message.
Examples
# Create a basic logger
logger <- Logger$new()
logger$info("This is an info message")
#> 2024-08-19T01:32:43.976Z INFO This is an info message
logger$warn("This is a warning")
#> 2024-08-19T01:32:43.981Z WARNING This is a warning
logger$error("This is an error")
#> 2024-08-19T01:32:44.005Z ERROR This is an error
# Create a logger with custom settings, message formatting, and context
custom_logger <- Logger$new(
level = LogLevel$WARNING,
file_path = tempfile("log_"),
print_fn = function(x) message(paste0("Custom: ", x)),
format_fn = function(level, msg) paste0("Hello prefix: ", msg),
context = list(program = "MyApp")
)
custom_logger$info("This won't be logged")
custom_logger$warn("This will be logged with a custom prefix")
#> Custom: 2024-08-19T01:32:44.010Z WARNING Hello prefix: This will be logged with a custom prefix
#> Context:
#> {
#> "program": "MyApp"
#> }
# Change log level and update context
custom_logger$set_level(LogLevel$INFO)
custom_logger$update_context(list(user = "John"))
custom_logger$info("Now this will be logged with a custom prefix and context")
#> Custom: 2024-08-19T01:32:44.014Z INFO Hello prefix: Now this will be logged with a custom prefix and context
#> Context:
#> {
#> "program": "MyApp",
#> "user": "John"
#> }
## ------------------------------------------------
## Method `Logger$set_level`
## ------------------------------------------------
logger <- Logger$new()
logger$set_level(LogLevel$WARNING)
## ------------------------------------------------
## Method `Logger$error`
## ------------------------------------------------
logger <- Logger$new()
logger$error("An error occurred", data = list(x = 1), error = "Oops!")
#> 2024-08-19T01:32:44.018Z ERROR An error occurred
#> Data:
#> {
#> "x": 1
#> }
#> Error:
#> "Oops!"
## ------------------------------------------------
## Method `Logger$warn`
## ------------------------------------------------
logger <- Logger$new()
logger$warn("This is a warning", data = list(reason = "example"))
#> 2024-08-19T01:32:44.022Z WARNING This is a warning
#> Data:
#> {
#> "reason": "example"
#> }
## ------------------------------------------------
## Method `Logger$info`
## ------------------------------------------------
logger <- Logger$new()
logger$info("Operation completed successfully", data = list(duration = 5.2))
#> 2024-08-19T01:32:44.025Z INFO Operation completed successfully
#> Data:
#> {
#> "duration": 5.2
#> }