fix logger

This commit is contained in:
anlicheng 2023-09-07 20:05:37 +08:00
parent 7742eb3046
commit 8c95474cc0
2 changed files with 26 additions and 15 deletions

View File

@ -21,7 +21,6 @@
-define(LOG_FILE, "north_data").
-record(state, {
root_dir :: string(),
file_path :: string(),
file
}).
@ -49,19 +48,11 @@ start_link() ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
RealFile = make_file(),
RootDir = code:root_dir() ++ "/log/",
FilePath = RootDir ++ RealFile,
lager:debug("root_dir: ~p", [FilePath]),
case filelib:is_dir(RootDir) of
false ->
file:make_dir(RootDir);
true ->
ok
end,
ensure_dir(),
FilePath = make_file(),
{ok, File} = file:open(FilePath, [append, binary]),
{ok, #state{file = File, root_dir = RootDir, file_path = FilePath}}.
{ok, #state{file = File, file_path = FilePath}}.
%% @private
%% @doc Handling call messages
@ -82,9 +73,9 @@ 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{root_dir = RootDir, file_path = OldFilePath, file = OldFile}) ->
handle_cast({write, Data}, State = #state{file_path = OldFilePath, file = OldFile}) ->
Line = format(Data),
FilePath = RootDir ++ make_file(),
FilePath = make_file(),
case FilePath =:= OldFilePath of
true ->
ok = file:write(OldFile, Line),
@ -143,4 +134,14 @@ time_prefix() ->
make_file() ->
{Year, Month, Day} = erlang:date(),
Prefix = io_lib:format("~b~2..0b~2..0b-", [Year, Month, Day]),
lists:flatten(Prefix ++ ?LOG_FILE).
RootDir = code:root_dir() ++ "/log/",
lists:flatten(RootDir ++ Prefix ++ ?LOG_FILE).
ensure_dir() ->
RootDir = code:root_dir() ++ "/log/",
case filelib:is_dir(RootDir) of
true ->
ok;
false ->
file:make_dir(RootDir)
end.

View File

@ -28,6 +28,15 @@ start_link() ->
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
Specs = [
#{
id => 'iot_logger',
start => {'iot_logger', start_link, []},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['iot_logger']
}
#{
id => 'iot_endpoint_sup',
start => {'iot_endpoint_sup', start_link, []},
@ -54,6 +63,7 @@ init([]) ->
type => supervisor,
modules => ['iot_host_sup']
}
],
{ok, {SupFlags, pools() ++ Specs}}.