Skip to content

Commit

Permalink
fix(log-rotate): should rotate logs strictly hourly(or minutely) (#6521)
Browse files Browse the repository at this point in the history
Co-authored-by: wangsong <wangsong@taou.com>
Co-authored-by: 帅进超 <shuaijinchao@gmail.com>
  • Loading branch information
3 people committed Mar 16, 2022
1 parent 91b57e6 commit cbd9263
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
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);
$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

0 comments on commit cbd9263

Please sign in to comment.