nulllib-lua/logging.lua

58 lines
1.5 KiB
Lua

logging = {}
local loglevels={
["SILENT"]=0,
["FATAL"]=1,
["ERROR"]=2,
["WARN"]=3,
["INFO"]=4,
["DEBUG"]=5,
["TRACE"]=6
}
local loglevels_index={}
local loglevels_abbrev={}
for k, v in pairs(loglevels) do
loglevels_index[v]=k
loglevels_abbrev[k:sub(1,1)]=v
end
local loglevel
---@deprecated use loglevel int
local loglevel_str="INFO"
---@param level string|integer
function logging.set(level)
-- +---we can do this
-- | becasue each one has
-- v a unique letter
loglevel=loglevels[level] or loglevels_abbrev[tostring(level):sub(1,1):upper()] or
((level >=0 and level <=6) and level) or loglevel
end
---deprecated wrapper for logging.set, it is intentionally a wrapper instead of
--an alias because LuaLS docs don't work without doing this
---@deprecated user logging.set instead, more terse
function logging.setLogLevel(level)
return logging.set(level)
end
-- default log level
logging.set("INFO")
local function printLog(severity, ...)
if (loglevel) >= severity then
log("[" .. loglevels_index[severity] .. "] ", ...)
end
end
function logging.fatal(...) printLog(1, ...) end
function logging.error(...) printLog(2, ...) end
function logging.warn(...) printLog(3, ...) end
function logging.info(...) printLog(4, ...) end
function logging.debug(...) printLog(5, ...) end
function logging.trace(...) printLog(6, ...) end
function logging.test(...) printLog(1, ...) end
return logging