打印module如下:log.lua
-
#!/usr/bin/lua
-
-
-- level 打印等级,4是开启所有打印,0是关闭所有打印
-
-- output 打印输出位置,stdout(标准输出),file(输出到文件)
-
-- path 日志输出路径
-
-- package.loaded["log"] = log, require "log"返回module名log
-
-
local M = {
-
level = 4,
-
output = "stdout",
-
path = "./log"
-
}
-
-
log = M
-
-
function M.configure(level, output, path)
-
if level ~= nil then M.level = level end
-
if output ~= nil then M.output = output end
-
if path ~= nil then M.path = path end
-
end
-
-
function M.debug(fmt, ...)
-
if (M.level >= 4) then
-
M.generalPrint(1, fmt, ...)
-
end
-
end
-
-
function M.info(fmt, ...)
-
if (M.level >= 3) then
-
M.generalPrint(2, fmt, ...)
-
end
-
end
-
-
function M.warn(fmt, ...)
-
if (M.level >= 2) then
-
M.generalPrint(3, fmt, ...)
-
end
-
end
-
-
function M.error(fmt, ...)
-
if (M.level >= 1) then
-
M.generalPrint(4, fmt, ...)
-
end
-
end
-
-
function M.generalPrint(level, fmt, ...)
-
if (level >= 0) and (level <= 4) then
-
if (select("#", ...) > 0) then
-
local t = {}
-
for i=1, select("#", ...) do
-
t[i] = tostring(select(i, ...))
-
end
-
s = string.format(fmt, unpack(t))
-
else
-
s = fmt
-
end
-
-
local line = debug.getinfo(3).currentline
-
local file = string.gsub(debug.getinfo(3).short_src, ".+/", "")
-
local func = debug.getinfo(3).name
-
local timenow = os.date("%Y-%m-%d %H:%M:%S")
-
local str
-
-
if level == 1 then
-
str = string.format("%s \27[0;32;32m[DEBUG]\27[m[%s, %-4d][%-15s] \27[0;32;32m%s\27[m\n",
-
timenow, tostring(file), tostring(line), tostring(func), tostring(s))
-
elseif level == 2 then
-
str = string.format("%s \27[0;32;33m[INFO ]\27[m[%s, %-4d][%-15s] \27[0;32;33m%s\27[m\n",
-
timenow, tostring(file), tostring(line), tostring(func), tostring(s))
-
elseif level == 3 then
-
str = string.format("%s \27[0;32;35m[WARN ]\27[m[%s, %-4d][%-15s] \27[0;32;35m%s\27[m\n",
-
timenow, tostring(file), tostring(line), tostring(func), tostring(s))
-
elseif level == 4 then
-
str = string.format("%s \27[0;32;31m[ERROR]\27[m[%s, %-4d][%-15s] \27[0;32;31m%s\27[m\n",
-
timenow, tostring(file), tostring(line), tostring(func), tostring(s))
-
end
-
-
if log.output == "file" then
-
local f = assert(io.open(log.path, 'a+'))
-
f:write(str)
-
f:close()
-
elseif log.output == "stdout" then
-
io.write(str)
-
end
-
end
-
end
-
-
return log
测试,test.lua
-
local log = require "log"
-
-
-- log.configure(4, "file", "./log") --打印等级4,输出到文件./log中
-
-
function test()
-
log.debug("debug (%s)", nil)
-
log.info("info (%s)", 10)
-
log.warn("warn (%s)", "wo")
-
log.error("error (%s)", {"ok"})
-
end
-
-
log.debug("debug (%s)", nil)
-
log.info("info (%s)", 10)
-
log.warn("warn (%s)", "wo")
-
log.error("error (%s)", {"ok"})
-
test()
结果:
-
[root@localhost lua]# lua test.lua
-
2017-03-30 09:32:10 [DEBUG][test.lua, 480 ][nil ] debug (nil)
-
2017-03-30 09:32:10 [INFO ][test.lua, 481 ][nil ] info (10)
-
2017-03-30 09:32:10 [WARN ][test.lua, 482 ][nil ] warn (wo)
-
2017-03-30 09:32:10 [ERROR][test.lua, 483 ][nil ] error (table: 0x8992f30)
-
2017-03-30 09:32:10 [DEBUG][test.lua, 474 ][test ] debug (nil)
-
2017-03-30 09:32:10 [INFO ][test.lua, 475 ][test ] info (10)
-
2017-03-30 09:32:10 [WARN ][test.lua, 476 ][test ] warn (wo)
-
2017-03-30 09:32:10 [ERROR][test.lua, 477 ][test ] error (table: 0x89964a8)
注意:
1.任何变量都可以使用%s打印出来,不需要使用string.format()。
2.如果是在函数外部加载的打印,则函数名为nil。
3.可以指定打印的等级(log.level = 4),打印到文件中(log.path = "./log",log.output = "file"), 打印到标注输出(log.output = "stdout")。
阅读(2441) | 评论(0) | 转发(0) |