diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 61bcea163a08..14e4c45c71ee 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -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 diff --git a/t/plugin/log-rotate3.t b/t/plugin/log-rotate3.t index 8e7b92eee65a..3f1edd95a37b 100644 --- a/t/plugin/log-rotate3.t +++ b/t/plugin/log-rotate3.t @@ -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