log v1.5.0

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.

Installation

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua log
View on GitHub →

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:

wget https://raw.githubusercontent.com/Twijn/cc-misc/main/util/log.lua

Examples

Using with Runtime Installation

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

Usage Examples

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

Functions

module.debug(msg)

View source on GitHub

Log a debug message in gray (only to file, not console by default)

Parameters:

module.info(msg)

View source on GitHub

Log an informational message in blue

Parameters:

module.warn(msg)

View source on GitHub

Log a warning message in yellow

Parameters:

module.error(msg)

View source on GitHub

Log an error message in red

Parameters:

module.critical(msg)

View source on GitHub

Log a critical/crash message in red This is always logged both to console and file, and immediately flushed

Parameters:

module.flush()

View source on GitHub

Flush any pending log entries to disk

module.setLevel(level)

View source on GitHub

Set the current log level Messages with levels more verbose than this will only be written to file, not console

Parameters:
Returns: string? error Error message if failed

module.getLevel()

View source on GitHub

Get the current log level name

Returns: string levelName The current log level name

module.getLevels()

View source on GitHub

Get all available log levels

Returns: table levels Table with level names as keys and numbers as values

module.registerCommands(commands)

View source on GitHub

Register log level commands with a cmd command table This adds "loglevel" command with aliases "log-level" and "ll"

Parameters:
Returns: table commands The modified commands table (also modifies in place)

module.configureDiskManagement(settings)

View source on GitHub

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)

Parameters:

module.getDiskStats()

View source on GitHub

Get disk space statistics for the log directory

Returns: table stats Statistics about log storage

module.cleanupOldLogs(maxAgeDays?)

View source on GitHub

Manually trigger cleanup of old log files

Parameters:
Returns: number deleted Number of files deleted