Prints
--- Configuration options for print/logging functions.
-- @field enabled Enables or disables all logging.
-- @field info Enables informational messages.
-- @field warn Enables warning messages.
-- @field error Enables error messages.
-- @field success Enables success messages.
-- @field debug Enables debug messages.
local options = {
enabled = true,
info = true,
warn = true,
error = true,
success = true,
debug = true,
}
local prefixes = {
info = '[^4INFO^7]',
warn = '[^3WARN^7]',
error = '[^1ERROR^7]',
success = '[^2SUCCESS^7]',
debug = '[^6DEBUG^7]',
}
local template = '^7%s - %s^7'
local function handleException(reason, value)
if type(value) == 'function' then return tostring(value) end
return reason
end
local jsonOptions = { sort_keys = true, indent = true, exception = handleException }
local function log(level, fmt, ...)
if not options.enabled or not options[level] then return end
local args = { ... }
if #args == 0 then
print(template:format(prefixes[level], fmt))
return
end
for i = 1, #args do
local arg = args[i]
args[i] = type(arg) == 'table' and json.encode(arg, jsonOptions) or tostring(arg)
end
local message = string.format(fmt, table.unpack(args))
print(template:format(prefixes[level], message))
end
prints = {
info = function(fmt, ...) log('info', fmt, ...) end,
warn = function(fmt, ...) log('warn', fmt, ...) end,
error = function(fmt, ...) log('error', fmt, ...) end,
success = function(fmt, ...) log('success', fmt, ...) end,
debug = function(fmt, ...) log('debug', fmt, ...) end,
}
Last updated
Was this helpful?