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): should rotate logs strictly hourly(or minutely) #6521

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ local function rotate()
local now_time = ngx_time()
if not rotate_time then
-- first init rotate time
rotate_time = now_time + interval
rotate_time = now_time + interval - (now_time % interval)
core.log.info("first init rotate time is: ", rotate_time)
return
end
Expand Down
51 changes: 45 additions & 6 deletions t/plugin/log-rotate2.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ no_root_location();
add_block_preprocessor(sub {
my ($block) = @_;

my $user_yaml_config = <<_EOC_;
if (!defined $block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
admin_key: null

plugins: # plugin list
admin_key: ~
plugins:
- log-rotate

plugin_attr:
log-rotate:
interval: 1
max_kept: 3
enable_compression: true
_EOC_

$block->set_value("yaml_config", $user_yaml_config);
Copy link
Member

Choose a reason for hiding this comment

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

We can check if yaml_config exists before setting it

Copy link
Member

Choose a reason for hiding this comment

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

updated

$block->set_value("yaml_config", $yaml_config);
}

if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]");
Expand Down Expand Up @@ -138,3 +138,42 @@ start xxxxxx
}
--- response_body
passed



=== TEST 4: test rotate time align
--- yaml_config
apisix:
node_listen: 1984
admin_key: ~
plugins:
- log-rotate
plugin_attr:
log-rotate:
interval: 3600
max_kept: 1
--- config
location /t {
content_by_lua_block {
local log_file = ngx.config.prefix() .. "logs/error.log"
local file = io.open(log_file, "r")
local log = file:read("*a")

local m, err = ngx.re.match(log, [[first init rotate time is: (\d+)]], "jom")
if not m then
ngx.log(ngx.ERR, "failed to gmatch: ", err)
return
end

ngx.sleep(2)

local now_time = ngx.time()
local interval = 3600
local rotate_time = now_time + interval - (now_time % interval)
if tonumber(m[1]) == tonumber(rotate_time) then
ngx.say("passed")
end
}
}
--- response_body
passed