fix logger

This commit is contained in:
anlicheng 2024-09-10 14:40:41 +08:00
parent 20ea8178e2
commit c8e4497a20
3 changed files with 216 additions and 5 deletions

View File

@ -16,7 +16,9 @@
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle_request("GET", "/api/hello", _, _) ->
handle_request("POST", "/api/device_info", _, Params) ->
njau_bot_logger:write(jiffy:encode(Params, [force_utf8])),
{ok, 200, http_protocol:json_data(<<"hello world">>)};
handle_request(_, Path, _, _) ->

View File

@ -0,0 +1,202 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 07. 9 2023 17:07
%%%-------------------------------------------------------------------
-module(njau_bot_logger).
-author("aresei").
-behaviour(gen_server).
%% API
-export([start_link/1, write/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
%%
-define(BUFFER_SIZE, 100).
-record(state, {
file_name :: string(),
date :: calendar:date(),
file,
buffer = []
}).
%%%===================================================================
%%% API
%%%===================================================================
write(Data) ->
gen_server:cast(?SERVER, {write, Data}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_link(FileName :: string()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(FileName) when is_list(FileName) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [FileName], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
%% @doc Initializes the server
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([FileName]) ->
ensure_dir(),
FilePath = make_file(FileName),
{ok, File} = file:open(FilePath, [append, binary]),
erlang:start_timer(5000, self(), flush_ticker),
{ok, #state{file = File, file_name = FileName, date = get_date()}}.
%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #state{}) ->
{reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.
%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast({write, Data}, State = #state{buffer = Buffer}) ->
Line = <<(time_prefix())/binary, " ", (format(Data))/binary, $\n>>,
NBuffer = [Line|Buffer],
case length(NBuffer) >= ?BUFFER_SIZE of
true ->
{noreply, flush(State#state{buffer = NBuffer})};
false ->
{noreply, State#state{buffer = NBuffer}}
end.
%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info({timeout, _, flush_ticker}, State) ->
erlang:start_timer(5000, self(), flush_ticker),
{noreply, flush(State)};
handle_info(_Info, State = #state{}) ->
{noreply, State}.
%% @private
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()).
terminate(_Reason, _State = #state{}) ->
ok.
%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec flush(State :: #state{}) -> NState :: #state{}.
flush(State = #state{buffer = []}) ->
State;
flush(State = #state{file = OldFile, file_name = FileName, date = Date, buffer = Buffer}) ->
Content = iolist_to_binary(lists:reverse(Buffer)),
case maybe_new_file(Date) of
true ->
file:close(OldFile),
FilePath = make_file(FileName),
{ok, File} = file:open(FilePath, [append, binary]),
ok = file:write(File, Content),
%% ,
delete_old_files(FileName, 30),
State#state{file = File, buffer = [], date = get_date()};
false ->
ok = file:write(OldFile, Content),
State#state{buffer = []}
end.
format(Data) when is_binary(Data) ->
iolist_to_binary(Data);
format(Items) when is_list(Items) ->
iolist_to_binary(lists:join(<<"\t">>, Items)).
time_prefix() ->
{{Y, M, D}, {H, I, S}} = calendar:local_time(),
iolist_to_binary(io_lib:format("[~b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b]", [Y, M, D, H, I, S])).
-spec make_file(LogFile :: string()) -> string().
make_file(LogFile) when is_list(LogFile) ->
Date = erlang:date(),
make_file(LogFile, Date).
make_file(LogFile, {Year, Month, Day}) when is_list(LogFile) ->
Suffix = io_lib:format("~b~2..0b~2..0b", [Year, Month, Day]),
RootDir = code:root_dir() ++ "/log/",
lists:flatten(RootDir ++ LogFile ++ "." ++ Suffix).
ensure_dir() ->
RootDir = code:root_dir() ++ "/log/",
case filelib:is_dir(RootDir) of
true ->
ok;
false ->
file:make_dir(RootDir)
end.
%%
-spec get_date() -> Date :: calendar:date().
get_date() ->
{Date, _} = calendar:local_time(),
Date.
%%
-spec maybe_new_file(Date :: calendar:date()) -> boolean().
maybe_new_file({Y, M, D}) ->
{{Y0, M0, D0}, _} = calendar:local_time(),
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),
case filelib:is_file(FilePath) of
true ->
file:delete(FilePath);
false ->
ok
end
end, lists:seq(1, 10)).

View File

@ -26,10 +26,17 @@ start_link() ->
%% type => worker(), % optional
%% modules => modules()} % optional
init([]) ->
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1},
ChildSpecs = [],
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
ChildSpecs = [
#{
id => njau_bot_logger,
start => {'njau_bot_logger', start_link, ["bot_data"]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['njau_bot_logger']
}
],
{ok, {SupFlags, ChildSpecs}}.
%% internal functions