This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.lua
65 lines (52 loc) · 1.35 KB
/
log.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local log = {}
local loglevels = {
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
"PRINT",
}
local revlevels = {}
for i, level in ipairs(loglevels) do revlevels[level] = i end
local startuploglevel = os and os.getenv
and revlevels[string.upper(os.getenv("LUALOG") or "")]
or revlevels.INFO
-- TODO: provide interfaces to dynamically set log level
local currentloglevel = startuploglevel
local function defaultlogger(from, level, ...)
if level >= currentloglevel then
local components = {}
for _, v in ipairs({...}) do table.insert(components,tostring(v)) end
print(string.format(
"[%s]%s %s",
loglevels[level],
from and "("..from..")" or "",
table.concat(components, "\t")
))
end
end
local currentlogger = defaultlogger -- TODO: allow overriding
function log.print(...)
currentlogger(nil, revlevels.PRINT, ...)
end
function log.fatal(...)
currentlogger(nil, revlevels.FATAL, ...)
end
function log.error(...)
currentlogger(nil, revlevels.ERROR, ...)
end
function log.warn(...)
currentlogger(nil, revlevels.WARN, ...)
end
function log.info(...)
currentlogger(nil, revlevels.INFO, ...)
end
function log.debug(...)
currentlogger(nil, revlevels.DEBUG, ...)
end
function log.trace(...)
currentlogger(nil, revlevels.TRACE, ...)
end
return log