This commit is contained in:
anlicheng 2024-09-05 15:59:59 +08:00
parent 9b5e4d604c
commit ad5588ab05

View File

@ -138,15 +138,7 @@ flush(State = #state{file = OldFile, file_name = FileName, date = Date, buffer =
ok = file:write(File, Content),
%% ,
OldFiles = list_old_files(FileName, 30),
[begin
case filelib:is_file(FilePath) of
true ->
file:delete(FilePath);
false ->
ok
end
end || FilePath <- OldFiles],
delete_old_files(FileName, 30),
State#state{file = File, buffer = [], date = get_date()};
false ->
@ -194,11 +186,17 @@ maybe_new_file({Y, M, D}) ->
{{Y0, M0, D0}, _} = calendar:local_time(),
not (Y =:= Y0 andalso M =:= M0 andalso D =:= D0).
-spec list_old_files(LogFile :: string(), Days :: integer()) -> [string()].
list_old_files(LogFile, Days) when is_list(LogFile), is_integer(Days) ->
-spec delete_old_files(FileName :: string(), Days :: integer()) -> no_return().
delete_old_files(FileName, Days) when is_list(FileName), is_integer(Days) ->
Seconds0 = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
Seconds = Seconds0 - Days * 86400,
lists:map(fun(Day) ->
lists:foreach(fun(Day) ->
{Date, _} = calendar:gregorian_seconds_to_datetime(Seconds - Day * 86400),
make_file(LogFile, Date)
FilePath = make_file(FileName, Date),
case filelib:is_file(FilePath) of
true ->
file:delete(FilePath);
false ->
ok
end
end, lists:seq(1, 10)).