diff --git a/apisix/plugins/error-log-logger.lua b/apisix/plugins/error-log-logger.lua index 70d5b1cd3341..d7b04b65f4a7 100644 --- a/apisix/plugins/error-log-logger.lua +++ b/apisix/plugins/error-log-logger.lua @@ -25,7 +25,6 @@ local table = core.table local schema_def = core.schema local ngx = ngx local tcp = ngx.socket.tcp -local string = string local tostring = tostring local ipairs = ipairs local lrucache = core.lrucache.new({ @@ -122,12 +121,12 @@ end local function update_filter(value) - local level = log_level[string.upper(value.level)] + local level = log_level[value.level] local status, err = errlog.set_filter_level(level) if not status then return nil, "failed to set filter level by ngx.errlog, the error is :" .. err else - core.log.debug("set the filter_level to ", config.level) + core.log.notice("set the filter_level to ", value.level) end return value @@ -149,12 +148,17 @@ local function process() end + local err_level = log_level[metadata.value.level] local entries = {} local logs = errlog.get_logs(9) while ( logs and #logs>0 ) do for i = 1, #logs, 3 do - table.insert(entries, logs[i + 2]) - table.insert(entries, "\n") + -- There will be some stale error logs after the filter level changed. + -- We should avoid reporting them. + if logs[i] <= err_level then + table.insert(entries, logs[i + 2]) + table.insert(entries, "\n") + end end logs = errlog.get_logs(9) end diff --git a/t/plugin/error-log-logger.t b/t/plugin/error-log-logger.t index 0fc9f2cd6273..2d2792f90af7 100644 --- a/t/plugin/error-log-logger.t +++ b/t/plugin/error-log-logger.t @@ -327,7 +327,44 @@ qr/please set the correct plugin_metadata for error-log-logger/ -=== TEST 10: delete the route +=== TEST 10: avoid sending stale error log +--- yaml_config +apisix: + enable_admin: true + admin_key: null +plugins: + - error-log-logger +--- config + location /tg { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + core.log.warn("this is a warning message for test.") + local code, body = t('/apisix/admin/plugin_metadata/error-log-logger', + ngx.HTTP_PUT, + [[{ + "host": "127.0.0.1", + "port": 1999, + "level": "ERROR", + "inactive_timeout": 1 + }]] + ) + ngx.sleep(2) + core.log.error("this is an error message for test.") + } + } +--- request +GET /tg +--- response_body +--- no_error_log eval +qr/\[Server\] receive data:.*this is a warning message for test./ +--- error_log eval +qr/\[Server\] receive data:.*this is an error message for test./ +--- wait: 5 + + + +=== TEST 11: delete the route --- yaml_config apisix: enable_admin: true