diff --git a/apps/iot/src/iot_logger.erl b/apps/iot/src/iot_logger.erl index feed296..79ff5bd 100644 --- a/apps/iot/src/iot_logger.erl +++ b/apps/iot/src/iot_logger.erl @@ -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). \ No newline at end of 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. \ No newline at end of file diff --git a/apps/iot/src/iot_sup.erl b/apps/iot/src/iot_sup.erl index bd20919..93b395e 100644 --- a/apps/iot/src/iot_sup.erl +++ b/apps/iot/src/iot_sup.erl @@ -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}}.