简化日志文件

This commit is contained in:
anlicheng 2025-03-27 16:04:24 +08:00
parent ecc5f19760
commit 859aea44f2

View File

@ -13,7 +13,6 @@
%% API
-export([start_link/1, start_link/2, write/2]).
-export([archive_file/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -26,7 +25,7 @@
%%
queue = queue:new(),
counter = 5,
file
file :: file:io_device()
}).
%%%===================================================================
@ -61,7 +60,7 @@ init([FileName, Counter]) ->
FilePath = make_file(FileName, erlang:date()),
{ok, IoDevice} = file:open(FilePath, [append, binary]),
{ok, #state{file = {IoDevice, FilePath}, queue = queue:from_list([FilePath]), file_name = FileName, counter = Counter, date = get_date()}}.
{ok, #state{file = IoDevice, queue = queue:from_list([FilePath]), file_name = FileName, counter = Counter, date = get_date()}}.
%% @private
%% @doc Handling call messages
@ -82,16 +81,13 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, queue = Q, counter = Counter, file_name = FileName, date = Date}) ->
handle_cast({write, Data}, State = #state{file = OldIoDevice, queue = Q, counter = Counter, file_name = FileName, date = Date}) ->
Line = iolist_to_binary([time_prefix(), <<" ">>, format(Data), <<$\n>>]),
NDate = erlang:date(),
case maybe_new_file(Date, NDate) of
true ->
ok = file:close(OldIoDevice),
%%
erlang:spawn(fun() -> archive_file(OldFilePath) end),
%%
NewFilePath = make_file(FileName, NDate),
{ok, NewIoDevice} = file:open(NewFilePath, [append, binary]),
@ -105,7 +101,7 @@ handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, que
false ->
queue:in(NewFilePath, Q)
end,
{noreply, State#state{file = {NewIoDevice, NewFilePath}, queue = NQ, date = get_date()}};
{noreply, State#state{file = NewIoDevice, queue = NQ, date = get_date()}};
false ->
ok = file:write(OldIoDevice, Line),
{noreply, State}
@ -175,27 +171,4 @@ get_date() ->
%%
-spec maybe_new_file(Date :: calendar:date(), Date0 :: calendar:date()) -> boolean().
maybe_new_file({Y, M, D}, {Y0, M0, D0}) ->
not (Y =:= Y0 andalso M =:= M0 andalso D =:= D0).
%-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:foreach(fun(Day) ->
% {Date, _} = calendar:gregorian_seconds_to_datetime(Seconds - Day * 86400),
% FilePath = make_file(FileName, Date),
% filelib:is_file(FilePath) andalso file:delete(FilePath)
% end, lists:seq(1, 10)).
-spec archive_file(FilePath :: string()) -> ok.
archive_file(FilePath) when is_list(FilePath) ->
TarFile = FilePath ++ ".tar",
{ok, Tar} = erl_tar:open(TarFile, [write, compressed]),
BaseName = filename:basename(FilePath),
{ok, Data} = file:read_file(FilePath),
ok = erl_tar:add(Tar, Data, BaseName, []),
ok = erl_tar:close(Tar),
%%
ok = file:delete(FilePath).
not (Y =:= Y0 andalso M =:= M0 andalso D =:= D0).