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 12:34:47 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #2097: feature: implemented plugin `log-rotate`, rotate log by interval time.

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