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): skip access log when enable_access_log is set to false #11310

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,7 +77,6 @@ end


local function get_log_path_info(file_type)
local_conf = core.config.local_conf()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this line?

Copy link
Contributor Author

@flearc flearc Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local_conf is initialized in the init function now.

local conf_path
if file_type == "error.log" then
conf_path = local_conf and local_conf.nginx_config and
Expand Down Expand Up @@ -189,10 +189,8 @@ end
local function init_default_logs(logs_info, log_type)
local filepath, filename = get_log_path_info(log_type)
logs_info[log_type] = { type = log_type }
if filename ~= "off" then
flearc marked this conversation as resolved.
Show resolved Hide resolved
logs_info[log_type].file = filepath .. filename
logs_info[log_type].new_file = filepath .. "/%s__" .. filename
end
logs_info[log_type].file = filepath .. filename
logs_info[log_type].new_file = filepath .. "/%s__" .. filename
end


Expand All @@ -210,7 +208,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like unrelated change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_files stores the name of access.log and error.log. If enable_access_log is set to false, nginx will not generate access.log file, no need to pre-allocate an array of length 2.

-- rename the log files
for _, file in ipairs(files) do
local now_date = os_date("%Y-%m-%d_%H-%M-%S", now_time)
Expand Down Expand Up @@ -290,21 +288,24 @@ 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
rotate_time = rotate_time + interval

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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why make this change? It seems unrelated to me. If it really is, you should remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same that no need to pre-allocate. It only needs to store one filename when enable_access_log is false.


if access_log_file_size >= max_size then
if enable_access_log and file_size(default_logs[DEFAULT_ACCESS_LOG_FILENAME].file) >= max_size then
core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME)
end

local error_log_file_size = file_size(default_logs[DEFAULT_ERROR_LOG_FILENAME].file)
if error_log_file_size >= max_size then
core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME)
end
Expand All @@ -315,6 +316,11 @@ end


function _M.init()
if not local_conf then
local_conf = core.config.local_conf()
end
local nginx_config = local_conf and local_conf.nginx_config
flearc marked this conversation as resolved.
Show resolved Hide resolved
enable_access_log = nginx_config and nginx_config.http and nginx_config.http.enable_access_log
timers.register_timer("plugin#log-rotate", rotate, true)
end

Expand Down
35 changes: 35 additions & 0 deletions t/plugin/log-rotate3.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading