Stenographer: R6 Class for Advanced Logging Functionality

StenographerR Documentation

R6 Class for Advanced Logging Functionality

Description

Provides a flexible logging system with support for multiple output destinations, customisable formatting, and contextual logging. Features include:

* Multiple log levels (ERROR, WARNING, INFO) * File-based logging * Database logging support * Customisable message formatting * Contextual data attachment * Coloured console output

Methods

Public methods


Method new()

Create a new Stenographer instance

Usage
Stenographer$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 'Stenographer' object.


Method set_level()

Update the minimum logging level

Usage
Stenographer$set_level(level)
Arguments
level

New log level (see 'LogLevel')


Method update_context()

Add or update contextual data

Usage
Stenographer$update_context(new_context)
Arguments
new_context

List of context key-value pairs


Method clear_context()

Remove all contextual data

Usage
Stenographer$clear_context()

Method get_context()

Retrieve current context data

Usage
Stenographer$get_context()
Returns

List of current context


Method error()

Log an error message

Usage
Stenographer$error(msg, data = NULL, error = NULL)
Arguments
msg

Error message text

data

Optional data to attach

error

Optional error object


Method warn()

Log a warning message

Usage
Stenographer$warn(msg, data = NULL)
Arguments
msg

Warning message text

data

Optional data to attach


Method info()

Log an informational message

Usage
Stenographer$info(msg, data = NULL)
Arguments
msg

Info message text

data

Optional data to attach


Method clone()

The objects of this class are cloneable with this method.

Usage
Stenographer$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

# Create a basic Stenographer
steno <- Stenographer$new()
steno$info("This is an info message")
steno$warn("This is a warning")
steno$error("This is an error")

# Disable all logging
steno$set_level(LogLevel$OFF)
steno$info("This won't be logged")
steno$warn("This won't be logged either")
steno$error("This also won't be logged")

# Create a logger with custom settings, message formatting, and context
custom_steno <- Stenographer$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_steno$info("This won't be logged")
custom_steno$warn("This will be logged with a custom prefix")

# Change log level and update context
custom_steno$set_level(LogLevel$INFO)
custom_steno$update_context(list(user = "John"))
custom_steno$info("Now this will be logged with a custom prefix and context")


stenographer documentation built on April 4, 2025, 4:55 a.m.