Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304888
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-13 10:58
文章分类

全部博文(82)

文章存档

2018年(2)

2017年(9)

2016年(71)

我的朋友

分类: 嵌入式

2017-03-30 09:39:19

打印module如下:log.lua

  1. #!/usr/bin/lua

  2. -- level 打印等级,4是开启所有打印,0是关闭所有打印
  3. -- output 打印输出位置,stdout(标准输出),file(输出到文件)
  4. -- path 日志输出路径
  5. -- package.loaded["log"] = log, require "log"返回module名log

  6. local M = {
  7.     level = 4,
  8.     output = "stdout",
  9.     path = "./log"
  10. }

  11. log = M

  12. function M.configure(level, output, path)
  13.     if level ~= nil then M.level = level end
  14.     if output ~= nil then M.output = output end
  15.     if path ~= nil then M.path = path end
  16. end

  17. function M.debug(fmt, ...)
  18.     if (M.level >= 4) then
  19.         M.generalPrint(1, fmt, ...)
  20.     end
  21. end

  22. function M.info(fmt, ...)
  23.     if (M.level >= 3) then
  24.         M.generalPrint(2, fmt, ...)
  25.     end
  26. end

  27. function M.warn(fmt, ...)
  28.     if (M.level >= 2) then
  29.         M.generalPrint(3, fmt, ...)
  30.     end
  31. end

  32. function M.error(fmt, ...)
  33.     if (M.level >= 1) then
  34.         M.generalPrint(4, fmt, ...)
  35.     end
  36. end

  37. function M.generalPrint(level, fmt, ...)
  38.     if (level >= 0) and (level <= 4) then
  39.         if (select("#", ...) > 0) then
  40.             local t = {}
  41.             for i=1, select("#", ...) do
  42.                 t[i] = tostring(select(i, ...))
  43.             end
  44.             s = string.format(fmt, unpack(t))
  45.         else
  46.             s = fmt
  47.         end
  48.         
  49.         local line = debug.getinfo(3).currentline
  50.         local file = string.gsub(debug.getinfo(3).short_src, ".+/", "")
  51.         local func = debug.getinfo(3).name
  52.         local timenow = os.date("%Y-%m-%d %H:%M:%S")
  53.         local str    
  54.         
  55.         if level == 1 then
  56.             str = string.format("%s \27[0;32;32m[DEBUG]\27[m[%s, %-4d][%-15s] \27[0;32;32m%s\27[m\n",
  57.                                 timenow, tostring(file), tostring(line), tostring(func), tostring(s))
  58.         elseif level == 2 then
  59.             str = string.format("%s \27[0;32;33m[INFO ]\27[m[%s, %-4d][%-15s] \27[0;32;33m%s\27[m\n",
  60.                                 timenow, tostring(file), tostring(line), tostring(func), tostring(s))
  61.         elseif level == 3 then
  62.             str = string.format("%s \27[0;32;35m[WARN ]\27[m[%s, %-4d][%-15s] \27[0;32;35m%s\27[m\n",
  63.                                 timenow, tostring(file), tostring(line), tostring(func), tostring(s))
  64.         elseif level == 4 then
  65.             str = string.format("%s \27[0;32;31m[ERROR]\27[m[%s, %-4d][%-15s] \27[0;32;31m%s\27[m\n",
  66.                                 timenow, tostring(file), tostring(line), tostring(func), tostring(s))
  67.         end
  68.         
  69.         if log.output == "file" then
  70.             local f = assert(io.open(log.path, 'a+'))
  71.             f:write(str)
  72.             f:close()
  73.         elseif log.output == "stdout" then
  74.             io.write(str)
  75.         end
  76.     end
  77. end

  78. return log

测试,test.lua
  1. local log = require "log"

  2. -- log.configure(4, "file", "./log") --打印等级4,输出到文件./log中

  3. function test()
  4.     log.debug("debug (%s)", nil)
  5.     log.info("info (%s)", 10)
  6.     log.warn("warn (%s)", "wo")
  7.     log.error("error (%s)", {"ok"})
  8. end

  9. log.debug("debug (%s)", nil)
  10. log.info("info (%s)", 10)
  11. log.warn("warn (%s)", "wo")
  12. log.error("error (%s)", {"ok"})
  13. test()

结果:
  1. [root@localhost lua]# lua test.lua
  2. 2017-03-30 09:32:10 [DEBUG][test.lua, 480 ][nil ] debug (nil)
  3. 2017-03-30 09:32:10 [INFO ][test.lua, 481 ][nil ] info (10)
  4. 2017-03-30 09:32:10 [WARN ][test.lua, 482 ][nil ] warn (wo)
  5. 2017-03-30 09:32:10 [ERROR][test.lua, 483 ][nil ] error (table: 0x8992f30)
  6. 2017-03-30 09:32:10 [DEBUG][test.lua, 474 ][test ] debug (nil)
  7. 2017-03-30 09:32:10 [INFO ][test.lua, 475 ][test ] info (10)
  8. 2017-03-30 09:32:10 [WARN ][test.lua, 476 ][test ] warn (wo)
  9. 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) |
给主人留下些什么吧!~~