apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.5k stars 2.52k forks source link

help request: [file-logger] duplicate log, new log file contains history log message #7899

Closed kizy008 closed 2 years ago

kizy008 commented 2 years ago

Description

In my project, I need split log file by hour and delete logs 7 days ago. So I write a rotate plugin like [log-rotate.lua], and it works. But I found that every new log file contains the history log message. For example, [log-2022-09-01_01-00-00.log] contains all message in [log-2022-09-01_00-00-00.log]]. So every log contains all history message. I think the new log should contains message in its hour period. But I got all history message. I want to know how to get incremental logs in new log file.

我的项目中需要将日志按照小时分割并自动淘汰7天前的历史日志。 所以我仿照[log-rotate.lua]写了file-logger的滚动切割日志插件,并且按照预期生效了。但是我发现,所有的心日志文件都包含了全量的历史日志。比如 [log-2022-09-01_01-00-00.log]日志不仅包含了23点到0点的日志, 还包含了所有的历史全量日志。我本以为新日志应该只包含过去1个小时之内的日志。 我想知道如何利用file-logger在每个日志文件中保存这个小时时间段内增量的日志呢?

Environment

AlphaChen commented 2 years ago

i meet same problem。can u show u code?

monkeyDluffy6017 commented 2 years ago
  1. rename old log file and create new one
  2. notify other processes to reopen the file

Do you follow the two steps ?

AlphaChen commented 2 years ago
  1. rename old log file and create new one
  2. notify other processes to reopen the file

Do you follow the two steps ?

how to notify other processes

monkeyDluffy6017 commented 2 years ago

You could use ngx.shared.dict to notify other processes. For example

-- process 1
local rotate = ngx.shared.rotate
rotate:set("reopen", true)
-- process 2
local rotate = ngx.shared.rotate
local reopen = rotate:get("reopen")
if reopen then
    -- reopen file
end
tzssangglass commented 2 years ago

Since this is your own code, not related to the project, I turned it into discussion.