Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(log-rotate): the max_kept configuration doesn't work when using custom name #9749

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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