mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-03 20:47:35 +08:00
feature: support to print log with specific prefix. (#1284)
This commit is contained in:
parent
12677c322b
commit
d7f54ff518
@ -14,12 +14,14 @@
|
||||
-- See the License for the specific language governing permissions and
|
||||
-- limitations under the License.
|
||||
--
|
||||
|
||||
local ngx = ngx
|
||||
local ngx_log = ngx.log
|
||||
local require = require
|
||||
local setmetatable = setmetatable
|
||||
|
||||
|
||||
local _M = {version = 0.3}
|
||||
local _M = {version = 0.4}
|
||||
|
||||
|
||||
local log_levels = {
|
||||
@ -38,6 +40,33 @@ local log_levels = {
|
||||
local cur_level = ngx.config.subsystem == "http" and
|
||||
require "ngx.errlog" .get_sys_filter_level()
|
||||
local do_nothing = function() end
|
||||
|
||||
|
||||
function _M.new(prefix)
|
||||
local m = {version = _M.version}
|
||||
setmetatable(m, {__index = function(self, cmd)
|
||||
local log_level = log_levels[cmd]
|
||||
|
||||
local method
|
||||
if cur_level and (log_level > cur_level)
|
||||
then
|
||||
method = do_nothing
|
||||
else
|
||||
method = function(...)
|
||||
return ngx_log(log_level, prefix, ...)
|
||||
end
|
||||
end
|
||||
|
||||
-- cache the lazily generated method in our
|
||||
-- module table
|
||||
m[cmd] = method
|
||||
return method
|
||||
end})
|
||||
|
||||
return m
|
||||
end
|
||||
|
||||
|
||||
setmetatable(_M, {__index = function(self, cmd)
|
||||
local log_level = log_levels[cmd]
|
||||
|
||||
|
24
t/core/log.t
24
t/core/log.t
@ -148,3 +148,27 @@ warn log
|
||||
notice log
|
||||
info log
|
||||
debug log
|
||||
|
||||
|
||||
|
||||
=== TEST 6: print error log with prefix
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local log = require("apisix.core").log.new("test: ")
|
||||
log.error("error log")
|
||||
log.warn("warn log")
|
||||
log.notice("notice log")
|
||||
log.info("info log")
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
--- log_level: error
|
||||
--- request
|
||||
GET /t
|
||||
--- error_log
|
||||
error log
|
||||
--- no_error_log
|
||||
test: warn log
|
||||
test: notice log
|
||||
test: info log
|
||||
|
Loading…
Reference in New Issue
Block a user