Log
Provides a structured logging implementation for the Lyra library.
Design:
-
Callback-based: Instead of directly printing or sending logs, this module
uses a callback function (
logCallback) provided during logger creation. This allows the consuming application to decide how and where log messages are routed (e.g., print to console, send to an external service, store in memory). -
Structured Context: Log messages include a
contexttable. Loggers can beextended with additional context fields, which are automatically merged into every subsequent log message created by that logger instance or its descendants. This helps provide detailed, structured information for debugging and monitoring. -
Log Levels: Supports standard log levels (
fatal,error,warn,info,debug,trace). A global log level can be set usingLog.setLevelto filter out messages below the desired severity.
Usage:
local Log = require(script.Parent.Log)
-- Set the global minimum log level (optional, defaults to "info")
Log.setLevel("debug")
-- Create a logger instance with a callback
local myLogger = Log.createLogger(function(logMessage)
print(`[{logMessage.level}] {logMessage.message}`, logMessage.context)
end, { initialContext = "value" })
-- Log messages
myLogger:log("info", "User logged in", { userId = 123 })
-- Create a logger with extended context
local sessionLogger = myLogger:extend({ sessionId = "abc" })
sessionLogger:log("debug", "Session data loaded")
-- Output will include { initialContext = "value", sessionId = "abc", userId = 123 }
-- if logged via myLogger, or { initialContext = "value", sessionId = "abc" }
-- if logged via sessionLogger.
Types
LogLevel
enumtype LogLevel = "fatal" | "error" | "warn" | "info" | "debug" | "trace"Represents the different log levels available for logging messages.
LogMessage
interface LogMessage {message: string--
The main content of the log message.
context: {[string]: any}?--
Optional table containing additional structured context.
}Represents a log message sent to the logger's callback function.
Functions
setLevel
Sets the global minimum log level. Messages with a severity lower than this level will be ignored by all loggers.
Errors
| Type | Description |
|---|---|
| string | Throws an error if the provided level is invalid. |
createLogger
Log.createLogger(logCallback: (logMessage: LogMessage) → (),--
The function that will be called for each log message that passes the level filter. This function receives the complete LogMessage object including merged context.
context: {[string]: any}?--
Optional initial context for this logger.
) → Logger--
A new Logger instance.
Factory function to create a new root Logger instance.
log
Log:log(message: string,--
The log message content.
context: {[string]: any}?--
Optional additional context specific to this log call.
) → ()Logs a message if its level is at or above the globally set log level.
Merges the provided context table with the logger's persistent context
before calling the configured _logCallback.
extend
Log:extend(context: {[string]: any}--
The additional context fields to add.
) → Logger--
A new Logger instance with the extended context.
Creates a new Logger instance that inherits the parent's callback but has an extended context.
The new logger's context is a merged table containing the parent's context
and the additional context provided here.