This is an automated email from the ASF dual-hosted git repository. baoyuan 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 81caea1e4 fix(log-rotate): skip access log when enable_access_log is set to false (#11310) 81caea1e4 is described below commit 81caea1e4b02809e1202bcea20529948d8aeac4f Author: fl <fleash...@gmail.com> AuthorDate: Thu Jul 17 09:19:54 2025 +0800 fix(log-rotate): skip access log when enable_access_log is set to false (#11310) --- apisix/plugins/log-rotate.lua | 17 ++++++++++++----- t/plugin/log-rotate3.t | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 4b0f32753..f90226d89 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -37,6 +37,7 @@ local str_byte = string.byte local ngx_sleep = require("apisix.core.utils").sleep local string_rfind = require("pl.stringx").rfind local local_conf +local enable_access_log local plugin_name = "log-rotate" @@ -76,7 +77,6 @@ end local function get_log_path_info(file_type) - local_conf = core.config.local_conf() local conf_path if file_type == "error.log" then conf_path = local_conf and local_conf.nginx_config and @@ -187,6 +187,9 @@ end local function init_default_logs(logs_info, log_type) + local_conf = core.config.local_conf() + enable_access_log = core.table.try_read_attr( + local_conf, "nginx_config", "http", "enable_access_log") local filepath, filename = get_log_path_info(log_type) logs_info[log_type] = { type = log_type } if filename ~= "off" then @@ -210,7 +213,7 @@ local function rotate_file(files, now_time, max_kept, timeout) return end - local new_files = core.table.new(2, 0) + local new_files = core.table.new(#files, 0) -- rename the log files for _, file in ipairs(files) do local now_date = os_date("%Y-%m-%d_%H-%M-%S", now_time) @@ -290,7 +293,11 @@ local function rotate() end if now_time >= rotate_time then - local files = {DEFAULT_ACCESS_LOG_FILENAME, DEFAULT_ERROR_LOG_FILENAME} + local files = {DEFAULT_ERROR_LOG_FILENAME} + if enable_access_log then + core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME) + end + rotate_file(files, now_time, max_kept, timeout) -- reset rotate time @@ -299,9 +306,9 @@ local function rotate() elseif max_size > 0 then local access_log_file_size = file_size(default_logs[DEFAULT_ACCESS_LOG_FILENAME].file) local error_log_file_size = file_size(default_logs[DEFAULT_ERROR_LOG_FILENAME].file) - local files = core.table.new(2, 0) + local files = {} - if access_log_file_size >= max_size then + if enable_access_log and access_log_file_size >= max_size then core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME) end diff --git a/t/plugin/log-rotate3.t b/t/plugin/log-rotate3.t index 3f1edd95a..da4b20637 100644 --- a/t/plugin/log-rotate3.t +++ b/t/plugin/log-rotate3.t @@ -205,3 +205,38 @@ nginx_config: } --- response_body 2 + + + +=== TEST 6: do not rotate access log files when access log is disable +--- extra_yaml_config +plugins: + - log-rotate +plugin_attr: + log-rotate: + interval: 1 + max_kept: 2 + enable_compression: false +--- yaml_config +nginx_config: + http: + enable_access_log: false + access_log: logs/acc3.log +--- config + location /t { + access_log off; + + content_by_lua_block { + ngx.sleep(3) + local lfs = require("lfs") + local access_count = 0 + for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do + if string.match(file_name, "acc3.log$") then + access_count = access_count + 1 + end + end + ngx.say(access_count) + } + } +--- response_body +0