nulllib-lua/logging.lua

47 lines
1.1 KiB
Lua

logging = {}
local loglevels={
["SILENT"]=0,
["FATAL"]=1,
["ERROR"]=2,
["WARN"]=3,
["INFO"]=4,
["DEBUG"]=5,
["TRACE"]=6
}
local loglevels_index={}
for k, v in pairs(loglevels) do
loglevels_index[v]=k
end
-- default log level
local loglevel="INFO"
function logging.set(level)
loglevel=loglevels[level] 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
logging.set("INFO")
local function printLog(severity, ...)
if (loglevels[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