Skip to content

Commit

Permalink
fix(log-rotate): can not keep max files when using custom name (#9749)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 authored Jun 30, 2023
1 parent 3e31285 commit 5d649bf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
6 changes: 3 additions & 3 deletions apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ end
local function scan_log_folder(log_file_name)
local t = {}

local log_dir, _ = get_log_path_info(log_file_name)
local log_dir, log_name = get_log_path_info(log_file_name)

local compression_log_type = log_file_name .. COMPRESSION_FILE_SUFFIX
local compression_log_type = log_name .. COMPRESSION_FILE_SUFFIX
for file in lfs.dir(log_dir) do
local n = string_rfind(file, "__")
if n ~= nil then
local log_type = file:sub(n + 2)
if log_type == log_file_name or log_type == compression_log_type then
if log_type == log_name or log_type == compression_log_type then
core.table.insert(t, file)
end
end
Expand Down
73 changes: 73 additions & 0 deletions t/plugin/log-rotate3.t
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,76 @@ start xxxxxx
}
--- response_body
passed
=== TEST 4: max_kept effective on differently named compression files
--- extra_yaml_config
plugins:
- log-rotate
plugin_attr:
log-rotate:
interval: 1
max_kept: 1
enable_compression: true
--- yaml_config
nginx_config:
error_log: logs/err1.log
http:
access_log: logs/acc1.log
--- config
location /t {
error_log logs/err1.log info;
access_log logs/acc1.log;
content_by_lua_block {
ngx.sleep(3)
local lfs = require("lfs")
local count = 0
for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do
if string.match(file_name, ".tar.gz$") then
count = count + 1
end
end
--- only two compression file
ngx.say(count)
}
}
--- response_body
2
=== TEST 5: check whether new log files were created
--- extra_yaml_config
plugins:
- log-rotate
plugin_attr:
log-rotate:
interval: 1
max_kept: 0
enable_compression: false
--- yaml_config
nginx_config:
error_log: logs/err2.log
http:
access_log: logs/acc2.log
--- config
location /t {
error_log logs/err2.log info;
access_log logs/acc2.log;
content_by_lua_block {
ngx.sleep(3)
local lfs = require("lfs")
local count = 0
for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do
if string.match(file_name, "err2.log$") or string.match(file_name, "acc2.log$") then
count = count + 1
end
end
ngx.say(count)
}
}
--- response_body
2

0 comments on commit 5d649bf

Please sign in to comment.