格式化log输出enLog.lua
local _M = {}
local function printf(fmt, ...)
ngx.log(ngx.ERR, "VEGETA_CASB ########## " .. string.format(fmt, ...) .. " ##########")
end
_M.printf = printf
return _M
使用方法test.lua
enLog = require ("enLog")
function testFun()
local value = 1024
enLog.printf("this is a enlog print %d\n", 1)
enLog.printf("this is a enlog print %s\n", "hello")
enLog.printf("this is a enlog print %f\n", 1.25)
enLog.printf("this is a enlog print 0x%x\n", value)
end
输出结果如下:
2020/01/15 09:07:43 [error] 24236#0: *204 [lua] enLog.lua:5: printf(): VEGETA_CASB ########## this is a enlog print 1 ##########
2020/01/15 09:07:43 [error] 24236#0: *204 [lua] enLog.lua:5: printf(): VEGETA_CASB ########## this is a enlog print hello ##########
2020/01/15 09:07:43 [error] 24236#0: *204 [lua] enLog.lua:5: printf(): VEGETA_CASB ########## this is a enlog print 1.250000 ##########
2020/01/15 09:07:43 [error] 24236#0: *204 [lua] enLog.lua:5: printf(): VEGETA_CASB ########## this is a enlog print 0x400 ##########