fix logger
This commit is contained in:
parent
7742eb3046
commit
8c95474cc0
@ -21,7 +21,6 @@
|
|||||||
-define(LOG_FILE, "north_data").
|
-define(LOG_FILE, "north_data").
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
root_dir :: string(),
|
|
||||||
file_path :: string(),
|
file_path :: string(),
|
||||||
file
|
file
|
||||||
}).
|
}).
|
||||||
@ -49,19 +48,11 @@ start_link() ->
|
|||||||
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term()} | ignore).
|
{stop, Reason :: term()} | ignore).
|
||||||
init([]) ->
|
init([]) ->
|
||||||
RealFile = make_file(),
|
ensure_dir(),
|
||||||
RootDir = code:root_dir() ++ "/log/",
|
FilePath = make_file(),
|
||||||
FilePath = RootDir ++ RealFile,
|
|
||||||
lager:debug("root_dir: ~p", [FilePath]),
|
|
||||||
case filelib:is_dir(RootDir) of
|
|
||||||
false ->
|
|
||||||
file:make_dir(RootDir);
|
|
||||||
true ->
|
|
||||||
ok
|
|
||||||
end,
|
|
||||||
{ok, File} = file:open(FilePath, [append, binary]),
|
{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
|
%% @private
|
||||||
%% @doc Handling call messages
|
%% @doc Handling call messages
|
||||||
@ -82,9 +73,9 @@ handle_call(_Request, _From, State = #state{}) ->
|
|||||||
{noreply, NewState :: #state{}} |
|
{noreply, NewState :: #state{}} |
|
||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{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),
|
Line = format(Data),
|
||||||
FilePath = RootDir ++ make_file(),
|
FilePath = make_file(),
|
||||||
case FilePath =:= OldFilePath of
|
case FilePath =:= OldFilePath of
|
||||||
true ->
|
true ->
|
||||||
ok = file:write(OldFile, Line),
|
ok = file:write(OldFile, Line),
|
||||||
@ -143,4 +134,14 @@ time_prefix() ->
|
|||||||
make_file() ->
|
make_file() ->
|
||||||
{Year, Month, Day} = erlang:date(),
|
{Year, Month, Day} = erlang:date(),
|
||||||
Prefix = io_lib:format("~b~2..0b~2..0b-", [Year, Month, Day]),
|
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.
|
||||||
@ -28,6 +28,15 @@ start_link() ->
|
|||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
|
||||||
Specs = [
|
Specs = [
|
||||||
|
#{
|
||||||
|
id => 'iot_logger',
|
||||||
|
start => {'iot_logger', start_link, []},
|
||||||
|
restart => permanent,
|
||||||
|
shutdown => 2000,
|
||||||
|
type => worker,
|
||||||
|
modules => ['iot_logger']
|
||||||
|
}
|
||||||
|
|
||||||
#{
|
#{
|
||||||
id => 'iot_endpoint_sup',
|
id => 'iot_endpoint_sup',
|
||||||
start => {'iot_endpoint_sup', start_link, []},
|
start => {'iot_endpoint_sup', start_link, []},
|
||||||
@ -54,6 +63,7 @@ init([]) ->
|
|||||||
type => supervisor,
|
type => supervisor,
|
||||||
modules => ['iot_host_sup']
|
modules => ['iot_host_sup']
|
||||||
}
|
}
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
{ok, {SupFlags, pools() ++ Specs}}.
|
{ok, {SupFlags, pools() ++ Specs}}.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user