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
|
local pprint = require 'pprint'
local class = require 'r.class'
local qw = require 'r.qw'
local levels = qw"debug proto info warn error"
local Log = class()
function Log.make(cls, name, level)
return setmetatable({
name=name,
level = level or 'info',
},cls)
end
local colours = {debug="\x1b[38;5;8m",error="\x1b[31m"}
function Log.format(self, level, ...)
local vals = {} for i=1,select('#',...) do
local v = (select(i,...))
vals[i] = type(v) == 'string' and v or pprint.pformat(v)
end
return (colours[level] or '')
.. os.date"%Y-%m-%d %H:%M:%S%z"
.. ' ' .. level .. (" "):rep(5-#level)
.. ' [' .. self.name .. '] '
.. table.concat(vals,' ') .. '\x1b[0m' .. '\n'
end
function Log.msg(self,level,...)
if levels[level] < levels[self.level] then return end
io.stderr:write(self:format(level, ...))
io.stderr:flush()
end
for i,v in ipairs(levels) do
levels[v]=i
Log[v] = function(self,...) self:msg(v,...) end
end
Log.__call = Log.info
-- handle errors correctly
function Log.loop(self, cq)
local ok, err, code, thread, pollee, fd = cq:loop()
local msg = debug.traceback(thread, err)
self:error(err)
error('👉'..self.name..' '..msg,0)
end
return Log
|