From ad5588ab0559d1631404a4f6abafdabb98be1972 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 5 Sep 2024 15:59:59 +0800 Subject: [PATCH] fix --- apps/iot/src/iot_logger.erl | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/apps/iot/src/iot_logger.erl b/apps/iot/src/iot_logger.erl index 9ffda21..aff5c04 100644 --- a/apps/iot/src/iot_logger.erl +++ b/apps/iot/src/iot_logger.erl @@ -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) - end, lists:seq(1, 10)). \ No newline at end of file + FilePath = make_file(FileName, Date), + case filelib:is_file(FilePath) of + true -> + file:delete(FilePath); + false -> + ok + end + end, lists:seq(1, 10)). \ No newline at end of file