Skip to content

Commit

Permalink
logrotate 增加正则匹配模式
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Dec 4, 2024
1 parent 036d274 commit b1465e0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
30 changes: 23 additions & 7 deletions lualib/skynet-fly/logrotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ end
function M:set_month(month)
assert(not self.is_builder, "builded can`t use set_month")
assert(month >= 1 and month <= 12)
self.month = month
local cfg = self.cfg
cfg.month = month
return self
end
--[[
Expand All @@ -137,7 +138,8 @@ end
function M:set_day(day)
assert(not self.is_builder, "builded can`t use set_day")
assert(day >= 1 and day <= 31)
self.day = day
local cfg = self.cfg
cfg.day = day
return self
end
--[[
Expand All @@ -150,7 +152,8 @@ end
function M:set_hour(hour)
assert(not self.is_builder, "builded can`t use set_hour")
assert(hour >= 0 and hour <= 23)
self.hour = hour
local cfg = self.cfg
cfg.hour = hour
return self
end
--[[
Expand All @@ -163,7 +166,8 @@ end
function M:set_min(min)
assert(not self.is_builder, "builded can`t use set_min")
assert(min >= 0 and min <= 59)
self.min = min
local cfg = self.cfg
cfg.min = min
return self
end
--[[
Expand All @@ -176,7 +180,8 @@ end
function M:set_sec(sec)
assert(not self.is_builder, "builded can`t use set_sec")
assert(sec >= 0 and sec <= 59)
self.sec = sec
local cfg = self.cfg
cfg.sec = sec
return self
end

Expand All @@ -190,7 +195,8 @@ end
function M:set_wday(wday)
assert(not self.is_builder, "builded can`t use set_wday")
assert(wday >= 1 and wday <= 7)
self.wday = wday
local cfg = self.cfg
cfg.wday = wday
return self
end

Expand All @@ -204,7 +210,17 @@ end
function M:set_yday(yday)
assert(not self.is_builder, "builded can`t use set_yday")
assert(yday >= 1 and yday <= 366)
self.yday = yday
local cfg = self.cfg
cfg.yday = yday
return self
end

--设置保留文件整理匹配表达式
function M:set_back_pattern(back_pattern)
assert(not self.is_builder, "builded can`t use set_back_pattern")
assert(type(back_pattern) == 'string', "back_pattern not string")
local cfg = self.cfg
cfg.back_pattern = back_pattern
return self
end

Expand Down
9 changes: 9 additions & 0 deletions lualib/skynet-fly/utils/file_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,13 @@ function M.new_copy_file(is_dir)
}
end

--递归删除文件夹
function M.rmdir(dir_path)
if M.is_window() then
return os.execute("rmdir /S /Q " .. dir_path)
else
return os.execute("rm -rf " .. dir_path)
end
end

return M
47 changes: 31 additions & 16 deletions module/logrotate_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,21 @@ local function os_execute(cmd)
return isok
end

local function remove_file(file_path, is_dir)
if is_dir then
local success, err = file_util.rmdir(file_path)
if not success then
log.warn("remove dir err ", file_path, err)
end
else
local success, err = os.remove(file_path)
if not success then
log.warn("remove file err ", file_path, err)
end
end
end

local function create_rotate(cfg)
assert(cfg.filename,"not filename")
local m_filename = cfg.filename --文件名
local m_rename_format = cfg.rename_format or "%Y%m%d" --重命名文件格式
local m_file_path = cfg.file_path or './' --文件路径
Expand All @@ -45,11 +58,15 @@ local function create_rotate(cfg)
local m_sec = cfg.sec or 0 --几秒
local m_wday = cfg.wday or 1 --周几
local m_yday = cfg.yday or 1 --一年第几天

local m_back_pattern = cfg.back_pattern --保留文件整理的查找pattern

local m_timer_obj = nil --定时器对象

m_rename_format = m_rename_format .. '_' .. m_filename
log.info("rotate format name :",os.date(m_rename_format,time_util.time()))
if m_filename then
m_rename_format = m_rename_format .. '_' .. m_filename
log.info("rotate format name :",os.date(m_rename_format,time_util.time()))
end
--切割
local function rotate()
local file_url = file_util.path_join(m_file_path, m_filename)
Expand Down Expand Up @@ -99,12 +116,15 @@ local function create_rotate(cfg)
end

local back_list = {}
for file_name,file_path,file_info, errmsg, errno in file_util.diripairs(m_file_path, 0) do
if file_name ~= m_filename and string.find(file_name,m_filename,nil,true) then

for file_name, file_path, file_info, errmsg, errno in file_util.diripairs(m_file_path, 0) do
if (m_filename and file_name ~= m_filename and string.find(file_name,m_filename,nil,true)) --按文件名整理
or (m_back_pattern and string.find(file_name, m_back_pattern)) then --按back_pattern整理
if file_info then
tinsert(back_list, {
file_path = file_path,
time = file_info.modification --最近一次修改时间
time = file_info.modification, --最近一次修改时间
is_dir = file_info.mode == 'directory',
})
else
log.warn("backup file can`t get file_info ", file_path, errmsg, errno)
Expand All @@ -114,15 +134,11 @@ local function create_rotate(cfg)

--最新的在前面
tsort(back_list,function(a,b) return a.time > b.time end)

--保留文件数
for i = #back_list,m_max_backups + 1, -1 do
--删除文件
local f = tremove(back_list,i)
local success, err = os.remove(f.file_path)
if not success then
log.warn("remove file err ", f.file_path, err)
end
remove_file(f.file_path, f.is_dir)
end

local cur_time = os.time()
Expand All @@ -132,10 +148,7 @@ local function create_rotate(cfg)
local f = back_list[i]
--过期了
if cur_time - f.time > max_age_time then
local success, err = os.remove(f.file_path)
if not success then
log.warn("remove file err ", f.file_path, err)
end
remove_file(f.file_path, f.is_dir)
else
--有序的,当前这个没过期,前面的肯定也没有过期
break
Expand All @@ -152,7 +165,9 @@ local function create_rotate(cfg)
time_obj:set_wday(m_wday)
time_obj:set_yday(m_yday)
time_obj:builder(function()
rotate()
if m_filename then
rotate()
end
backup()
end)

Expand Down

0 comments on commit b1465e0

Please sign in to comment.