Logging in Lua

Here’s the translation of the Go logging example to Lua, formatted in Markdown suitable for Hugo:

Lua provides several ways to handle logging, but there’s no standard library equivalent to Go’s log package. However, we can demonstrate similar functionality using Lua’s built-in features and a simple custom logging module.

local log = {}

-- Simple function to get current timestamp
local function get_timestamp()
    return os.date("%Y/%m/%d %H:%M:%S")
end

-- Basic log function
function log.print(message)
    print(get_timestamp() .. " " .. message)
end

-- Log function with microseconds (simulated)
function log.print_micro(message)
    local micro = math.floor(os.clock() * 1000000) % 1000000
    print(string.format("%s.%06d %s", get_timestamp(), micro, message))
end

-- Log function with file and line information
function log.print_with_info(message)
    local info = debug.getinfo(2, "Sl")
    local file = info.short_src
    local line = info.currentline
    print(string.format("%s %s:%d: %s", get_timestamp(), file, line, message))
end

-- Custom logger with prefix
local function create_logger(prefix)
    return function(message)
        print(prefix .. get_timestamp() .. " " .. message)
    end
end

-- Main execution
log.print("standard logger")

log.print_micro("with micro")

log.print_with_info("with file/line")

local mylog = create_logger("my:")
mylog("from mylog")

mylog = create_logger("ohmy:")
mylog("from mylog")

-- Logging to a string buffer
local buffer = {}
local function buffer_log(message)
    table.insert(buffer, "buf:" .. get_timestamp() .. " " .. message)
end

buffer_log("hello")
print("from buflog:" .. table.concat(buffer, "\n"))

-- Structured logging (basic implementation)
local function structured_log(message, ...)
    local args = {...}
    local structured = {
        time = get_timestamp(),
        level = "INFO",
        msg = message
    }
    for i = 1, #args, 2 do
        structured[args[i]] = args[i+1]
    end
    print(string.format('{"time":"%s","level":"%s","msg":"%s"%s}',
        structured.time, structured.level, structured.msg,
        #args > 0 and "," .. table.concat(args, ",") or ""))
end

structured_log("hi there")
structured_log("hello again", "key", "val", "age", 25)

This Lua script demonstrates logging functionality similar to the Go example. Here’s a breakdown of the implementation:

  1. We create a simple log module with basic logging functions.
  2. The get_timestamp() function simulates the timestamp functionality.
  3. log.print() is equivalent to the standard logger in Go.
  4. log.print_micro() simulates microsecond precision (note that Lua doesn’t have built-in microsecond precision).
  5. log.print_with_info() includes file and line information using Lua’s debug library.
  6. create_logger() allows creating custom loggers with prefixes.
  7. We demonstrate logging to a buffer using a table and the table.concat() function.
  8. The structured_log() function provides a basic implementation of structured logging, similar to Go’s slog package.

When you run this script, you’ll see output similar to the following:

2023/08/22 10:45:16 standard logger
2023/08/22 10:45:16.904141 with micro
2023/08/22 10:45:16 logging.lua:40: with file/line
my:2023/08/22 10:45:16 from mylog
ohmy:2023/08/22 10:45:16 from mylog
from buflog:buf:2023/08/22 10:45:16 hello
{"time":"2023-08-22 10:45:16","level":"INFO","msg":"hi there"}
{"time":"2023-08-22 10:45:16","level":"INFO","msg":"hello again",key,val,age,25}

Note that the exact timestamps will depend on when you run the script. Also, the structured logging output is a simplified version and may not be fully JSON-compliant.

This example demonstrates how to implement similar logging functionality in Lua, even though it doesn’t have a standard logging library like Go does.