Skip to content

Commit

Permalink
fix(error-log-logger): avoid sending stale error log (#4690)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Aug 2, 2021
1 parent 5e74ae1 commit 9f01ef8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
14 changes: 9 additions & 5 deletions apisix/plugins/error-log-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 38 additions & 1 deletion t/plugin/error-log-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f01ef8

Please sign in to comment.