Skip to content

Commit

Permalink
fix(log-rotate): max_kept works on compression files (#8366)
Browse files Browse the repository at this point in the history
Fixes #8343
  • Loading branch information
tzssangglass authored Nov 23, 2022
1 parent 01b4b49 commit c49be66
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ local function scan_log_folder(log_file_name)

local log_dir, _ = get_log_path_info(log_file_name)

local compression_log_type = log_file_name .. COMPRESSION_FILE_SUFFIX
for file in lfs.dir(log_dir) do
local n = get_last_index(file, "__")
if n ~= nil then
local log_type = file:sub(n + 2)
if log_type == log_file_name then
if log_type == log_file_name or log_type == compression_log_type then
core.table.insert(t, file)
end
end
Expand Down
39 changes: 37 additions & 2 deletions t/plugin/log-rotate2.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ no_root_location();
add_block_preprocessor(sub {
my ($block) = @_;

my $extra_yaml_config = <<_EOC_;
if (! $block->extra_yaml_config) {
my $extra_yaml_config = <<_EOC_;
plugins:
- log-rotate
plugin_attr:
Expand All @@ -35,7 +36,9 @@ plugin_attr:
enable_compression: true
_EOC_

$block->set_value("extra_yaml_config", $extra_yaml_config);
$block->set_value("extra_yaml_config", $extra_yaml_config);
}


if (!defined $block->request) {
$block->set_value("request", "GET /t");
Expand Down Expand Up @@ -169,3 +172,35 @@ plugin_attr:
}
--- response_body
passed
=== TEST 5: max_kept effective on compression files
--- extra_yaml_config
plugins:
- log-rotate
plugin_attr:
log-rotate:
interval: 1
max_kept: 1
enable_compression: true
--- config
location /t {
content_by_lua_block {
ngx.sleep(3.5)
local has_split_access_file = false
local has_split_error_file = false
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, access.log.tar.gz and error.log.tar.gz
ngx.say(count)
}
}
--- response_body
2
--- timeout: 5

0 comments on commit c49be66

Please sign in to comment.