This is an automated email from the ASF dual-hosted git repository. membphis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push: new 01dc41c bugfix: fetch the current log level when needs. (#885) 01dc41c is described below commit 01dc41c8a6ac6acb5f7836b5d44a6cdc1dc90f94 Author: YuanSheng Wang <membp...@gmail.com> AuthorDate: Sat Nov 23 20:40:47 2019 +0800 bugfix: fetch the current log level when needs. (#885) * bugfix: fetch the current log level when needs. --- lua/apisix/core/log.lua | 70 +++++++++++++++-------- t/core/log.t | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+), 23 deletions(-) diff --git a/lua/apisix/core/log.lua b/lua/apisix/core/log.lua index d04c4f8..b0b60de 100644 --- a/lua/apisix/core/log.lua +++ b/lua/apisix/core/log.lua @@ -18,32 +18,33 @@ local ngx = ngx local ngx_log = ngx.log local ngx_DEBUG= ngx.DEBUG local DEBUG = ngx.config.debug --- todo: support stream module -local cur_level = ngx.config.subsystem == "http" and - require "ngx.errlog" .get_sys_filter_level() - -local _M = {version = 0.1} - - -for name, log_level in pairs({stderr = ngx.STDERR, - emerg = ngx.EMERG, - alert = ngx.ALERT, - crit = ngx.CRIT, - error = ngx.ERR, - warn = ngx.WARN, - notice = ngx.NOTICE, - info = ngx.INFO, }) do - if cur_level and log_level > cur_level then - _M[name] = function() end - else - _M[name] = function(...) - return ngx_log(log_level, ...) - end - end -end +local require = require + +local _M = {version = 0.3} + + +local log_levels = { + stderr = ngx.STDERR, + emerg = ngx.EMERG, + alert = ngx.ALERT, + crit = ngx.CRIT, + error = ngx.ERR, + warn = ngx.WARN, + notice = ngx.NOTICE, + info = ngx.INFO +} + + +do + local cur_level function _M.debug(...) + if not cur_level then + cur_level = ngx.config.subsystem == "http" and + require "ngx.errlog" .get_sys_filter_level() + end + if not DEBUG and cur_level and ngx_DEBUG > cur_level then return end @@ -51,5 +52,28 @@ function _M.debug(...) return ngx_log(ngx_DEBUG, ...) end +end -- do + + +setmetatable(_M, {__index = function(self, cmd) + local cur_level = ngx.config.subsystem == "http" and + require "ngx.errlog" .get_sys_filter_level() + local log_level = log_levels[cmd] + + local method + if cur_level and log_levels[cmd] > cur_level then + method = function() end + else + method = function(...) + return ngx_log(log_level, ...) + end + end + + -- cache the lazily generated method in our + -- module table + _M[cmd] = method + return method +end}) + return _M diff --git a/t/core/log.t b/t/core/log.t new file mode 100644 index 0000000..2754174 --- /dev/null +++ b/t/core/log.t @@ -0,0 +1,146 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(2); +no_long_string(); +no_root_location(); + +run_tests; + +__DATA__ + +=== TEST 1: error log +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("error log") + core.log.warn("warn log") + core.log.notice("notice log") + core.log.info("info log") + ngx.say("done") + } + } +--- log_level: error +--- request +GET /t +--- error_log +error log +--- no_error_log +warn log +notice log +info log + + + +=== TEST 2: warn log +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("error log") + core.log.warn("warn log") + core.log.notice("notice log") + core.log.info("info log") + ngx.say("done") + } + } +--- log_level: warn +--- request +GET /t +--- error_log +error log +warn log +--- no_error_log +notice log +info log + + + +=== TEST 3: notice log +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("error log") + core.log.warn("warn log") + core.log.notice("notice log") + core.log.info("info log") + ngx.say("done") + } + } +--- log_level: notice +--- request +GET /t +--- error_log +error log +warn log +notice log +--- no_error_log +info log + + + +=== TEST 4: info log +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("error log") + core.log.warn("warn log") + core.log.notice("notice log") + core.log.info("info log") + core.log.debug("debug log") + ngx.say("done") + } + } +--- log_level: info +--- request +GET /t +--- error_log +error log +warn log +notice log +info log +--- no_error_log +debug log + + + +=== TEST 5: debug log +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + core.log.error("error log") + core.log.warn("warn log") + core.log.notice("notice log") + core.log.info("info log") + core.log.debug("debug log") + ngx.say("done") + } + } +--- log_level: debug +--- request +GET /t +--- error_log +error log +warn log +notice log +info log +debug log