libs/logger.lua

1
local print;
2
if os.getenv("LAHTTP_DEBUG") then
3
	print = _G.print;
4
else
5
	print = function () end;
6
end
7
 
8
local select, tostring = select, tostring;
9
module "logger"
10
 
11
local function format(format, ...)
12
	local n, maxn = 0, #arg;
13
	return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end));
14
end
15
 
16
local function format(format, ...)
17
	local n, maxn = 0, select('#', ...);
18
	local arg = { ... };
19
	return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
20
end
21
 
22
function init(name)
23
	return function (level, message, ...)
24
		print(level, format(message, ...));
25
	end
26
end
27
 
28
return _M;