-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
6a7a5ff
d3f88a7
6c07b96
3afcb7f
2f63bb2
7bb61cb
f34647c
fd04395
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like unrelated change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
-- rename the log files | ||
for _, file in ipairs(files) do | ||
local now_date = os_date("%Y-%m-%d_%H-%M-%S", now_time) | ||
|
@@ -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 = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 | ||
|
@@ -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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove this line?
There was a problem hiding this comment.
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 theinit
function now.