Overview
The Logger library provides a simple yet powerful way to handle logging in ComputerCraft programs. It supports multiple log levels, colored console output, and file-based logging with automatic directory creation.
Installation
Install the library using gfetch:
gfetch manaphoenix/CC-Code/main/lib/logger.lua
Alternative: Use wget
wget https://raw.githubusercontent.com/manaphoenix/CC-Code/main/lib/logger.lua
Basic Usage
Get started with the default logger or create tagged loggers for different components of your application.
lua
Basic Usage
local logger = require("logger")
-- Basic logging
logger.info("Application started")
logger.warning("This is a warning")
logger.error("Something went wrong!")
-- Create a tagged logger
local netLog = logger.get("NETWORK")
netLog.info("Connected to server")
netLog.error("Connection timeout")
Methods
log (
level:
string,
message:
string,
tag:
string,
file:
string
) :
void
Logs a message with the specified level, optional tag, and optional file output.
Example Code
lua
local logger = require("logger")
logger.log("INFO", "This is an info message")
logger.log("ERROR", "This is an error", "NETWORK", "network_errors.log")
get (
tag:
string,
file:
string
) :
table
Creates a new logger instance that prefixes all messages with a tag.
Example Code
lua
local logger = require("logger")
local netLog = logger.get("NETWORK")
netLog.info("Initialized network module") -- Logs: [HH:MM:SS] [INFO] [NETWORK] Initialized network module
Log Levels (Shortcut Methods) (
) :
void
Convenience methods for each log level.
Example Code
lua
local logger = require("logger")
-- Available log levels (in increasing severity):
logger.debug("Debug information")
logger.info("Informational message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical error, application may terminate")
Examples
lua
Basic Logging
local logger = require("logger")
-- Configure logger
logger.setLogPath("/myapp/logs")
logger.setDefaultFile("app.log")
logger.setLevel("DEBUG") -- Show all messages
-- Basic logging
logger.debug("Debug information")
logger.info("Application started")
logger.warning("Disk space low")
logger.error("Failed to save file")
-- Log to a specific file
logger.info("User logged in", nil, "auth.log")
-- Create a tagged logger for a specific module
local dbLog = logger.get("DATABASE")
dbLog.info("Connected to database")
dbLog.error("Query failed")
lua
Error Handling with Logging
local logger = require("logger")
-- Create a logger for file operations
local fileLog = logger.get("FILE")
local function processFile(filename)
local file = fs.open(filename, "r")
if not file then
fileLog.error(string.format("Failed to open file: %s", filename))
return nil
end
fileLog.debug(string.format("Processing file: %s", filename))
-- File processing logic here
local success, err = pcall(function()
-- Simulate file processing
if filename:find("test") then
error("Test file processing failed")
end
end)
file.close()
if not success then
fileLog.error(string.format("Error processing %s: %s", filename, err))
return nil
end
return true
end
-- Usage
processFile("config.txt") -- Will log to default file
processFile("test.txt") -- Will log error to default file
Advanced Usage
lua
Configuration Options
local logger = require("logger")
-- Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
logger.setLevel("DEBUG") -- Show all messages
-- Toggle output destinations
logger.setOutput({
console = true, -- Enable/disable console output
file = true -- Enable/disable file output
})
-- Set log directory and default file
logger.setLogPath("/myapp/logs") -- Will be created if it doesn't exist
logger.setDefaultFile("app.log")
-- Toggle features
logger.setColors(true) -- Enable/disable colored output
logger.setTimestamp(true) -- Show/hide timestamps
lua
Advanced Usage
local logger = require("logger")
-- Configure default logger
logger.setLogPath("/myapp/logs")
-- Create loggers for different modules
local dbLogger = logger.get("DATABASE", "database.log")
local netLogger = logger.get("NETWORK", "network.log")
-- These will go to their respective files
dbLogger.info("Connected to database")
netLogger.info("Connected to server")
-- This will go to the default log file
logger.info("Application started")