A logging utility module for ComputerCraft that provides colored console output and automatic file logging with daily log rotation. Features: Color-coded console output (red for errors, yellow for warnings, blue for info), automatic daily log file creation and rotation, persistent log storage in log/ directory, timestamped log entries, buffered writes for performance, configurable log levels, and automatic cleanup of old log files when disk space is low.
Recommended: Install via installer (handles dependencies automatically):
This pattern downloads and runs libraries at runtime, automatically installing any that are missing:
-- Auto-install and require libraries
local libs = {"log"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true
for _, lib in ipairs(libs) do
if not fs.exists(libDir .. lib .. ".lua") then
allExist = false
break
end
end
if not allExist then
shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end
local log = require(libDir .. "log")
-- Use the library
-- (your code here)
Alternative: Direct download via wget:
This example shows how to download and use libraries with automatic installation:
-- Auto-install and require libraries
local libs = {"log"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true
for _, lib in ipairs(libs) do
if not fs.exists(libDir .. lib .. ".lua") then
allExist = false
break
end
end
if not allExist then
shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end
local log = require(libDir .. "log")
-- Use the library
local log = require("log")
log.debug("This is a debug message")
log.info("Server started")
log.warn("High memory usage detected")
log.error("Failed to connect to database")
log.setLevel("debug") -- Show all messages including debug
log.setLevel("warn") -- Show only warnings and errors
module.debug(msg)Log a debug message in gray (only to file, not console by default)
msg (string): The message to logmodule.info(msg)Log an informational message in blue
msg (string): The message to logmodule.warn(msg)Log a warning message in yellow
msg (string): The message to logmodule.error(msg)Log an error message in red
msg (string): The message to logmodule.critical(msg)Log a critical/crash message in red This is always logged both to console and file, and immediately flushed
msg (string): The message to logmodule.setLevel(level)Set the current log level Messages with levels more verbose than this will only be written to file, not console
level (string|number): The log level: "error", "warn", "info", "debug" (or 1-4)module.getLevel()Get the current log level name
module.getLevels()Get all available log levels
module.registerCommands(commands)Register log level commands with a cmd command table This adds "loglevel" command with aliases "log-level" and "ll"
commands (table): The commands table to add the log level command tomodule.configureDiskManagement(settings)Configure disk space management settings - minFreeSpace: Minimum free bytes before cleanup (default: 10000) - maxLogAgeDays: Delete logs older than this (default: 30) - checkInterval: Seconds between disk space checks (default: 60)
settings (table): Configuration table with optional fields:module.getDiskStats()Get disk space statistics for the log directory
module.cleanupOldLogs(maxAgeDays?)Manually trigger cleanup of old log files
maxAgeDays? (number): Optional max age in days (default: use configured value)