Skip to contents

An R6 class for flexible logging with customisable output, message formatting, and context.

Methods


Method new()

Create a new Logger object.

Usage

Logger$new(
  level = LogLevel$INFO,
  file_path = NULL,
  db_conn = NULL,
  table_name = "LOGS",
  print_fn = function(x) cat(x, "\n"),
  format_fn = function(level, msg) msg,
  context = list()
)

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.

Returns

A new `Logger` object.


Method set_level()

Set the minimum log level.

Usage

Logger$set_level(level)

Arguments

level

The new minimum log level to set.

Examples

logger <- Logger$new()
logger$set_level(LogLevel$WARNING)


Method update_context()

Update the logger's context

Usage

Logger$update_context(new_context)

Arguments

new_context

A list of new context items to add or update


Method clear_context()

Clear the logger's context

Usage

Logger$clear_context()


Method get_context()

Get the current context

Usage

Logger$get_context()


Method error()

Log an error message.

Usage

Logger$error(msg, data = NULL, error = NULL)

Arguments

msg

Character; the error message to log.

data

Optional; additional data to include in the log entry.

error

Optional; an error object to include in the log entry.

Examples

logger <- Logger$new()
logger$error("An error occurred", data = list(x = 1), error = "Oops!")


Method warn()

Log a warning message.

Usage

Logger$warn(msg, data = NULL)

Arguments

msg

Character; the warning message to log.

data

Optional; additional data to include in the log entry.

Examples

logger <- Logger$new()
logger$warn("This is a warning", data = list(reason = "example"))


Method info()

Log an info message.

Usage

Logger$info(msg, data = NULL)

Arguments

msg

Character; the info message to log.

data

Optional; additional data to include in the log entry.

Examples

logger <- Logger$new()
logger$info("Operation completed successfully", data = list(duration = 5.2))


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

# 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
#> }