feature: support to print log with specific prefix. (#1284)

This commit is contained in:
YuanSheng Wang 2020-03-31 10:25:05 +08:00 committed by GitHub
parent 12677c322b
commit d7f54ff518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 1 deletions

View File

@ -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]

View File

@ -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