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