Stenographer | R Documentation |
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
new()
Create a new Stenographer instance
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() )
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.
A new 'Stenographer' object.
set_level()
Update the minimum logging level
Stenographer$set_level(level)
level
New log level (see 'LogLevel')
update_context()
Add or update contextual data
Stenographer$update_context(new_context)
new_context
List of context key-value pairs
clear_context()
Remove all contextual data
Stenographer$clear_context()
get_context()
Retrieve current context data
Stenographer$get_context()
List of current context
error()
Log an error message
Stenographer$error(msg, data = NULL, error = NULL)
msg
Error message text
data
Optional data to attach
error
Optional error object
warn()
Log a warning message
Stenographer$warn(msg, data = NULL)
msg
Warning message text
data
Optional data to attach
info()
Log an informational message
Stenographer$info(msg, data = NULL)
msg
Info message text
data
Optional data to attach
clone()
The objects of this class are cloneable with this method.
Stenographer$clone(deep = FALSE)
deep
Whether to make a deep clone.
# 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.