diff --git a/apisix/core/config_local.lua b/apisix/core/config_local.lua index 1c17086dc6e2..897b4f2c194e 100644 --- a/apisix/core/config_local.lua +++ b/apisix/core/config_local.lua @@ -55,6 +55,7 @@ end -- local local_conf = core.config.local_conf() -- local fallback_sni = core.table.try_read_attr( -- local_conf, "apisix", "ssl", "fallback_sni") -- "a.test2.com" +-- -- `nil` is returned if the attribute doesn't exist function _M.local_conf(force) if not force and config_data then return config_data diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index 61bcea163a08..e4314622ebd3 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -46,8 +46,10 @@ local COMPRESSION_FILE_SUFFIX = ".tar.gz" -- compression file suffix local rotate_time local default_logs local enable_compression = false -local DEFAULT_ACCESS_LOG_FILENAME = "access.log" -local DEFAULT_ERROR_LOG_FILENAME = "error.log" +local DEFAULT_ACCESS_LOG_FILEPATH = "logs/access.log" +local DEFAULT_ERROR_LOG_FILEPATH = "logs/error.log" +local custom_access_log_filename +local custom_error_log_filename local schema = { type = "object", @@ -73,10 +75,11 @@ local function file_exists(path) end +-- get the log directory path local function get_log_path_info(file_type) local_conf = core.config.local_conf() local conf_path - if file_type == "error.log" then + if file_type == custom_error_log_filename or file_type == DEFAULT_ERROR_LOG_FILEPATH then conf_path = local_conf and local_conf.nginx_config and local_conf.nginx_config.error_log else @@ -101,7 +104,7 @@ local function get_log_path_info(file_type) end end - return prefix .. "logs/", file_type + return prefix , file_type end @@ -110,6 +113,7 @@ local function tab_sort_comp(a, b) end +-- scans the log directory and returns a sorted table of matching log files local function scan_log_folder(log_file_name) local t = {} @@ -131,6 +135,19 @@ local function scan_log_folder(log_file_name) end +local function get_custom_logfile_name() + local local_conf = core.config.local_conf() + + local custom_error_log_filename = + core.table.try_read_attr(local_conf, "nginx_config", "error_log") + local custom_access_log_filename = + core.table.try_read_attr(local_conf, "nginx_config", "http", "access_log") + + return custom_access_log_filename, custom_error_log_filename + +end + + local function rename_file(log, date_str) local new_file if not log.new_file then @@ -144,9 +161,11 @@ local function rename_file(log, date_str) return new_file end - local ok, err = os_rename(log.file, new_file) + local filename = log.file + + local ok, err = os_rename(filename, new_file) if not ok then - core.log.error("move file from ", log.file, " to ", new_file, + core.log.error("move file from ", filename, " to ", new_file, " res:", ok, " msg:", err) return end @@ -258,6 +277,8 @@ local function rotate() local max_kept = MAX_KEPT local max_size = MAX_SIZE local attr = plugin.plugin_attr(plugin_name) + local access_log_filename = custom_access_log_filename or DEFAULT_ACCESS_LOG_FILEPATH + local error_log_filename = custom_error_log_filename or DEFAULT_ERROR_LOG_FILEPATH if attr then interval = attr.interval or interval max_kept = attr.max_kept or max_kept @@ -272,8 +293,8 @@ local function rotate() if not default_logs then -- first init default log filepath and filename default_logs = {} - init_default_logs(default_logs, DEFAULT_ACCESS_LOG_FILENAME) - init_default_logs(default_logs, DEFAULT_ERROR_LOG_FILENAME) + init_default_logs(default_logs, access_log_filename) + init_default_logs(default_logs, error_log_filename) end ngx_update_time() @@ -286,23 +307,23 @@ local function rotate() end if now_time >= rotate_time then - local files = {DEFAULT_ACCESS_LOG_FILENAME, DEFAULT_ERROR_LOG_FILENAME} + local files = {access_log_filename, error_log_filename} rotate_file(files, now_time, max_kept) -- 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 access_log_file_size = file_size(default_logs[access_log_filename].file) + local error_log_file_size = file_size(default_logs[error_log_filename].file) local files = core.table.new(2, 0) if access_log_file_size >= max_size then - core.table.insert(files, DEFAULT_ACCESS_LOG_FILENAME) + core.table.insert(files, access_log_filename) end if error_log_file_size >= max_size then - core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME) + core.table.insert(files, error_log_filename) end rotate_file(files, now_time, max_kept) @@ -311,6 +332,7 @@ end function _M.init() + custom_access_log_filename, custom_error_log_filename = get_custom_logfile_name() timers.register_timer("plugin#log-rotate", rotate, true) end diff --git a/t/plugin/log-rotate.t b/t/plugin/log-rotate.t index 8e6250766789..881cf0cf93d7 100644 --- a/t/plugin/log-rotate.t +++ b/t/plugin/log-rotate.t @@ -52,6 +52,7 @@ __DATA__ --- config location /t { content_by_lua_block { + local io = require("io") ngx.log(ngx.ERR, "start xxxxxx") ngx.sleep(2.5) local has_split_access_file = false @@ -105,6 +106,7 @@ start xxxxxx --- config location /t { content_by_lua_block { + local io = require("io") ngx.sleep(0.5) local t = require("lib.test_admin").test local code, _, org_body = t('/apisix/admin/plugins/reload', diff --git a/t/plugin/log-rotate2.t b/t/plugin/log-rotate2.t index 76651dd7415f..6a54bddbb4ac 100644 --- a/t/plugin/log-rotate2.t +++ b/t/plugin/log-rotate2.t @@ -181,13 +181,13 @@ plugins: - log-rotate plugin_attr: log-rotate: - interval: 1 + interval: 2 max_kept: 1 enable_compression: true --- config location /t { content_by_lua_block { - ngx.sleep(3.5) + ngx.sleep(2.5) local has_split_access_file = false local has_split_error_file = false local lfs = require("lfs") diff --git a/t/plugin/log-rotate3.t b/t/plugin/log-rotate3.t index 8e7b92eee65a..f2a2c09652bc 100644 --- a/t/plugin/log-rotate3.t +++ b/t/plugin/log-rotate3.t @@ -132,3 +132,79 @@ 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: 2 + max_kept: 1 + enable_compression: true +--- yaml_config +nginx_config: + user: root + error_log: logs/err1.log + http: + access_log: logs/acc1.log +--- config + location /t { + content_by_lua_block { + ngx.sleep(2.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 + ngx.say(count) + } + } +--- response_body +2 +--- timeout: 5 + + + +=== TEST 5: check whether new log files were created +--- extra_yaml_config +plugins: + - log-rotate +plugin_attr: + log-rotate: + interval: 2 + max_kept: 1 + enable_compression: false +--- yaml_config +nginx_config: + user: root + error_log: logs/test-error.log + http: + access_log: logs/test-access.log +--- config + location /t { + content_by_lua_block { + ngx.sleep(2.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, "error.log$") or string.match(file_name, "access.log$") then + count = count + 1 + end + end + --- 5 files: 2 older log files and 2 newer + fake-server-access.log + ngx.say(count) + } + } +--- response_body +5 +--- timeout: 5