You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/08/21 09:09:31 UTC
[GitHub] [apisix] membphis opened a new pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
membphis opened a new pull request #2097:
URL: https://github.com/apache/apisix/pull/2097
### What this PR does / why we need it:
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
as title.
### Pre-submission checklist:
* [ ] Did you explain what problem does this PR solve? Or what new features have been added?
* [ ] Have you added corresponding test cases?
* [ ] Have you modified the corresponding document?
* [ ] Is this PR backward compatible?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
membphis commented on pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#issuecomment-680421071
@nic-chen I think we can merge this PR now
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r474656753
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
Review comment:
Doesn't it trigger a premature rotate when the timer is fired for the first time?
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = prefix .. "logs/" .. file_type
+ os.rename(file_path_org, file_path)
+ core.log.warn("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ for file in lfs.dir(prefix .. "logs/") do
+ local log_type = file:sub(-10)
+ if log_type == "access.log" then
+ table.insert(t.access, file)
+ elseif log_type == "_error.log" then
+ table.insert(t.error, file)
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or interval
+ end
+
+ local time = ngx.time()
+ if time % interval == 0 then
+ time = time - interval
+ else
+ time = time - time % interval
+ end
+
+ local date_str = os.date("%Y-%m-%d_%H-%M-%S", time)
+
+ local ok1 = rotate_file(date_str, "access.log")
+ local ok2 = rotate_file(date_str, "error.log")
+ if not ok1 and not ok2 then
+ return
+ end
+
+ core.log.warn("send USER1 signal to master process [", ngx.worker.pid(),
Review comment:
Do you mean `get_master_pid()`?
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
Review comment:
The comment is misleading, max kept isn't related with any time interval.
##########
File path: conf/config-default.yaml
##########
@@ -187,3 +187,8 @@ plugins: # plugin list
stream_plugins:
- mqtt-proxy
+
+plugin_attr:
+ log-rotate:
+ interval: 3600 # an hour
+ max_kept: 168 # 7 days
Review comment:
The comment is misleading, max kept isn't related with any time interval.
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
Review comment:
Note that user can customize their log paths.
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = prefix .. "logs/" .. file_type
+ os.rename(file_path_org, file_path)
+ core.log.warn("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ for file in lfs.dir(prefix .. "logs/") do
+ local log_type = file:sub(-10)
+ if log_type == "access.log" then
+ table.insert(t.access, file)
+ elseif log_type == "_error.log" then
+ table.insert(t.error, file)
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or interval
Review comment:
Should be `or max_kept`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476148338
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
Review comment:
it's ok
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r477029630
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,243 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+local string = string
+local local_conf
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- rotate interval (unit: second)
+local MAX_KEPT = 24 * 7 -- max number of log files will be kept
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function get_last_index(str, key)
+ local rev = string.reverse(str)
+ local _, idx = string.find(rev, key)
+ local n
+ if idx then
+ n = string.len(rev) - idx + 1
+ end
+
+ return n
+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
+ local_conf.nginx_config.error_log
+ else
+ conf_path = local_conf and local_conf.nginx_config and
+ local_conf.nginx_config.http and
+ local_conf.nginx_config.http.access_log
+ end
+
+ local prefix = ngx.config.prefix()
+
+ if conf_path then
+ local root = string.sub(conf_path, 1, 1)
Review comment:
Better to use `string.byte(conf_path, 1, 1)`, and compare it with ascii number.
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,243 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+local string = string
+local local_conf
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- rotate interval (unit: second)
+local MAX_KEPT = 24 * 7 -- max number of log files will be kept
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function get_last_index(str, key)
+ local rev = string.reverse(str)
+ local _, idx = string.find(rev, key)
Review comment:
Plain string find required.
And better to put this function as utility function too.
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,243 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+local string = string
+local local_conf
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- rotate interval (unit: second)
+local MAX_KEPT = 24 * 7 -- max number of log files will be kept
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function get_last_index(str, key)
+ local rev = string.reverse(str)
+ local _, idx = string.find(rev, key)
+ local n
+ if idx then
+ n = string.len(rev) - idx + 1
+ end
+
+ return n
+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
+ local_conf.nginx_config.error_log
+ else
+ conf_path = local_conf and local_conf.nginx_config and
+ local_conf.nginx_config.http and
+ local_conf.nginx_config.http.access_log
+ end
+
+ local prefix = ngx.config.prefix()
+
+ if conf_path then
+ local root = string.sub(conf_path, 1, 1)
+ -- relative path
+ if root ~= "/" then
+ conf_path = prefix .. conf_path
+ end
+ local n = get_last_index(conf_path, "/")
+ if n ~= nil then
+ local dir = string.sub(conf_path, 1, n)
+ local name = string.sub(conf_path, n + 1)
+ return dir, name
+ end
Review comment:
Better to move the stuff above into two function: `is_abs` and `split_path` in a utility file so that other people can reuse it.
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,243 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+local string = string
+local local_conf
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- rotate interval (unit: second)
+local MAX_KEPT = 24 * 7 -- max number of log files will be kept
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function get_last_index(str, key)
+ local rev = string.reverse(str)
+ local _, idx = string.find(rev, key)
+ local n
+ if idx then
+ n = string.len(rev) - idx + 1
+ end
+
+ return n
+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
+ local_conf.nginx_config.error_log
+ else
+ conf_path = local_conf and local_conf.nginx_config and
+ local_conf.nginx_config.http and
+ local_conf.nginx_config.http.access_log
+ end
+
+ local prefix = ngx.config.prefix()
+
+ if conf_path then
+ local root = string.sub(conf_path, 1, 1)
+ -- relative path
+ if root ~= "/" then
+ conf_path = prefix .. conf_path
+ end
+ local n = get_last_index(conf_path, "/")
+ if n ~= nil then
+ local dir = string.sub(conf_path, 1, n)
+ local name = string.sub(conf_path, n + 1)
Review comment:
if n == #conf_path, the filename is empty.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] moonming commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
moonming commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476256202
##########
File path: doc/zh-cn/plugins/log-rotate.md
##########
@@ -0,0 +1,82 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+- [English](../../plugins/log-rotate.md)
+
+# api7-log-rotate
Review comment:
api7?
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,233 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+local string = string
+local local_conf
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- rotate interval (unit: second)
+local MAX_KEPT = 24 * 7 -- max number of log files will be kept
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function get_last_index(str, key)
+ local rev = string.reverse(str)
+ local _, idx = string.find(rev, key)
+ local n
+ if idx then
+ n = string.len(rev) - idx + 1
+ end
+
+ return n
+end
+
+
+local function get_log_path_info(file_type)
+ local_conf = core.config.local_conf()
+ local confpath
+ if file_type == "error.log" then
+ confpath = local_conf and local_conf.nginx_config and
+ local_conf.nginx_config.error_log
+ else
+ confpath = local_conf and local_conf.nginx_config and
+ local_conf.nginx_config.http and
+ local_conf.nginx_config.http.access_log
+ end
+
+ if confpath then
+ local n = get_last_index(confpath, "/")
+ if n ~= nil then
+ local dir = string.sub(confpath, 1, n)
+ local name = string.sub(confpath, n + 1)
+ return dir, name
+ end
+ end
+
+ return prefix .. "logs/", file_type
+end
+
+
+local function rotate_file(date_str, file_type)
+ local log_dir, filename = get_log_path_info(file_type)
+
+ local file_path = log_dir .. date_str .. "__" .. filename
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = log_dir .. filename
+ os.rename(file_path_org, file_path)
+ core.log.info("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ local log_dir, access_name = get_log_path_info("access.log")
+ local _, error_name = get_log_path_info("error.log")
+
+ for file in lfs.dir(log_dir) do
+ local n = get_last_index(file, "__")
+ if n ~= nil then
+ local log_type = file:sub(n + 2)
+ if log_type == access_name then
+ table.insert(t.access, file)
+ elseif log_type == error_name then
+ table.insert(t.error, file)
+ end
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or max_kept
+ end
+
+ core.log.info("rotate interval:", interval)
+ core.log.info("rotate max keep:", max_kept)
+
+ local time = ngx.time()
+ if time % interval == 0 then
+ time = time - interval
+ else
+ time = time - time % interval
+ end
+
+ local date_str = os.date("%Y-%m-%d_%H-%M-%S", time)
+
+ local ok1 = rotate_file(date_str, "access.log")
+ local ok2 = rotate_file(date_str, "error.log")
+ if not ok1 and not ok2 then
+ return
+ end
+
+ core.log.warn("send USER1 signal to master process [", ngx.worker.pid(),
+ "] for reopening log file")
+ local ok, err = signal.kill(ngx.worker.pid(), signal.signum("USR1"))
Review comment:
ditto
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = prefix .. "logs/" .. file_type
+ os.rename(file_path_org, file_path)
+ core.log.warn("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ for file in lfs.dir(prefix .. "logs/") do
+ local log_type = file:sub(-10)
+ if log_type == "access.log" then
+ table.insert(t.access, file)
+ elseif log_type == "_error.log" then
+ table.insert(t.error, file)
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or interval
+ end
+
+ local time = ngx.time()
+ if time % interval == 0 then
+ time = time - interval
+ else
+ time = time - time % interval
+ end
+
+ local date_str = os.date("%Y-%m-%d_%H-%M-%S", time)
+
+ local ok1 = rotate_file(date_str, "access.log")
+ local ok2 = rotate_file(date_str, "error.log")
+ if not ok1 and not ok2 then
+ return
+ end
+
+ core.log.warn("send USER1 signal to master process [", ngx.worker.pid(),
Review comment:
No, only nginx master process accept the `USR1` signal.
##########
File path: apisix/init.lua
##########
@@ -72,6 +72,12 @@ function _M.http_init(args)
math.randomseed(seed)
parse_args(args)
core.id.init()
+
Review comment:
why not use `logrotate`? which is supported by system
##########
File path: conf/config-default.yaml
##########
@@ -187,3 +187,8 @@ plugins: # plugin list
stream_plugins:
- mqtt-proxy
Review comment:
no plugin for `log-rotate` in list?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476885895
##########
File path: apisix/init.lua
##########
@@ -72,6 +72,12 @@ function _M.http_init(args)
math.randomseed(seed)
parse_args(args)
core.id.init()
+
Review comment:
In a container environment, users need to configure `logrotate` and `crontab`, and `alpine` image needs to install `logrotate` separately, which feels a bit complicated.
I think it's ok to provide an optional plugin.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476147832
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = prefix .. "logs/" .. file_type
+ os.rename(file_path_org, file_path)
+ core.log.warn("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ for file in lfs.dir(prefix .. "logs/") do
+ local log_type = file:sub(-10)
+ if log_type == "access.log" then
+ table.insert(t.access, file)
+ elseif log_type == "_error.log" then
+ table.insert(t.error, file)
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or interval
+ end
+
+ local time = ngx.time()
+ if time % interval == 0 then
+ time = time - interval
+ else
+ time = time - time % interval
+ end
+
+ local date_str = os.date("%Y-%m-%d_%H-%M-%S", time)
+
+ local ok1 = rotate_file(date_str, "access.log")
+ local ok2 = rotate_file(date_str, "error.log")
+ if not ok1 and not ok2 then
+ return
+ end
+
+ core.log.warn("send USER1 signal to master process [", ngx.worker.pid(),
Review comment:
it works fine with `worker pid`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r475033975
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
Review comment:
> Note that user can customize their log paths.
good catch. we need to check log path config in `config.yaml` first.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476959088
##########
File path: conf/config-default.yaml
##########
@@ -187,3 +187,8 @@ plugins: # plugin list
stream_plugins:
- mqtt-proxy
Review comment:
that is right
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476888123
##########
File path: conf/config-default.yaml
##########
@@ -187,3 +187,8 @@ plugins: # plugin list
stream_plugins:
- mqtt-proxy
Review comment:
when put `log-rotate` in plugin list, it will be enabled. we don't enable it by default.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#issuecomment-680440696
> @nic-chen I think we can merge this PR now
ok,thanks. @membphis
@moonming do you have other suggestions ?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] nic-chen commented on pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
nic-chen commented on pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#issuecomment-680661321
@spacewander We urgently need this feature, I will merge it first, and will deal with the optimization items later. thank you very much.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] moonming merged pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
moonming merged pull request #2097:
URL: https://github.com/apache/apisix/pull/2097
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] membphis commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.
Posted by GitBox <gi...@apache.org>.
membphis commented on a change in pull request #2097:
URL: https://github.com/apache/apisix/pull/2097#discussion_r476968377
##########
File path: apisix/plugins/log-rotate.lua
##########
@@ -0,0 +1,183 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local process = require("ngx.process")
+local signal = require("resty.signal")
+local ngx = ngx
+local prefix = ngx.config.prefix()
+local lfs = require("lfs")
+local io = io
+local os = os
+local table = table
+local select = select
+local type = type
+
+
+local timer
+local plugin_name = "log-rotate"
+local INTERVAL = 60 * 60 -- one hour
+local MAX_KEPT = 24 * 7 -- 7 days
+local schema = {
+ type = "object",
+ properties = {},
+ additionalProperties = false,
+}
+
+
+local _M = {
+ version = 0.1,
+ priority = 100,
+ name = plugin_name,
+ schema = schema,
+}
+
+
+local function file_exists(path)
+ local file = io.open(path, "r")
+ if file then
+ file:close()
+ end
+ return file ~= nil
+end
+
+
+local function rotate_file(date_str, file_type)
+ local file_path = prefix .. "logs/" .. date_str .. "_" .. file_type
+ if file_exists(file_path) then
+ core.log.info("file exist: ", file_path)
+ return false
+ end
+
+ local file_path_org = prefix .. "logs/" .. file_type
+ os.rename(file_path_org, file_path)
+ core.log.warn("move file from ", file_path_org, " to ", file_path)
+ return true
+end
+
+
+local function tab_sort(a, b)
+ return a > b
+end
+
+
+local function scan_log_folder()
+ local t = {
+ access = {},
+ error = {},
+ }
+
+ for file in lfs.dir(prefix .. "logs/") do
+ local log_type = file:sub(-10)
+ if log_type == "access.log" then
+ table.insert(t.access, file)
+ elseif log_type == "_error.log" then
+ table.insert(t.error, file)
+ end
+ end
+
+ table.sort(t.access, tab_sort)
+ table.sort(t.error, tab_sort)
+ return t
+end
+
+
+local function try_attr(t, ...)
+ local count = select('#', ...)
+ for i = 1, count do
+ local attr = select(i, ...)
+ t = t[attr]
+ if type(t) ~= "table" then
+ return false
+ end
+ end
+
+ return true
+end
+
+
+local function rotate()
+ local local_conf = core.config.local_conf()
+ local interval = INTERVAL
+ local max_kept = MAX_KEPT
+ if try_attr(local_conf, "plugin_attr", "log-rotate") then
+ local attr = local_conf.plugin_attr["log-rotate"]
+ interval = attr.interval or interval
+ max_kept = attr.max_kept or interval
+ end
+
+ local time = ngx.time()
+ if time % interval == 0 then
+ time = time - interval
+ else
+ time = time - time % interval
+ end
+
+ local date_str = os.date("%Y-%m-%d_%H-%M-%S", time)
+
+ local ok1 = rotate_file(date_str, "access.log")
+ local ok2 = rotate_file(date_str, "error.log")
+ if not ok1 and not ok2 then
+ return
+ end
+
+ core.log.warn("send USER1 signal to master process [", ngx.worker.pid(),
Review comment:
@spacewander catch good, many thx
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org