This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new c49be6665 fix(log-rotate): max_kept works on compression files (#8366)
c49be6665 is described below

commit c49be6665f8e51dfb2ccba86ac85474104b91d35
Author: tzssangglass <[email protected]>
AuthorDate: Wed Nov 23 11:22:12 2022 +0800

    fix(log-rotate): max_kept works on compression files (#8366)
    
    Fixes https://github.com/apache/apisix/issues/8343
---
 apisix/plugins/log-rotate.lua |  3 ++-
 t/plugin/log-rotate2.t        | 39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua
index 60b1e3ddb..7380b0d1c 100644
--- a/apisix/plugins/log-rotate.lua
+++ b/apisix/plugins/log-rotate.lua
@@ -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
diff --git a/t/plugin/log-rotate2.t b/t/plugin/log-rotate2.t
index 93f21d9cd..76651dd74 100644
--- a/t/plugin/log-rotate2.t
+++ b/t/plugin/log-rotate2.t
@@ -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:
@@ -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");
@@ -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

Reply via email to