change logger
This commit is contained in:
parent
6b8466ba65
commit
3992c2eb0f
@ -74,7 +74,8 @@ handle_call(_Request, _From, State = #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{file_path = OldFilePath, file = OldFile}) ->
|
handle_cast({write, Data}, State = #state{file_path = OldFilePath, file = OldFile}) ->
|
||||||
Line = format(Data),
|
Line = <<(time_prefix())/binary, " ", (format(Data))/binary, $\n>>,
|
||||||
|
|
||||||
FilePath = make_file(),
|
FilePath = make_file(),
|
||||||
case FilePath =:= OldFilePath of
|
case FilePath =:= OldFilePath of
|
||||||
true ->
|
true ->
|
||||||
@ -83,7 +84,8 @@ handle_cast({write, Data}, State = #state{file_path = OldFilePath, file = OldFil
|
|||||||
false ->
|
false ->
|
||||||
file:close(OldFile),
|
file:close(OldFile),
|
||||||
{ok, File} = file:open(FilePath, [append, binary]),
|
{ok, File} = file:open(FilePath, [append, binary]),
|
||||||
{noreply, State#state{file = File}}
|
ok = file:write(File, Line),
|
||||||
|
{noreply, State#state{file_path = FilePath, file = File}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
@ -118,13 +120,9 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
format(Data) when is_binary(Data) ->
|
format(Data) when is_binary(Data) ->
|
||||||
TimePrefix = time_prefix(),
|
iolist_to_binary(Data);
|
||||||
NData = iolist_to_binary(Data),
|
|
||||||
<<TimePrefix/binary, " ", NData/binary, $\n>>;
|
|
||||||
format(Items) when is_list(Items) ->
|
format(Items) when is_list(Items) ->
|
||||||
TimePrefix = time_prefix(),
|
iolist_to_binary(lists:join(<<"\t">>, Items)).
|
||||||
Line = iolist_to_binary(lists:join(<<"\t">>, Items)),
|
|
||||||
<<TimePrefix/binary, " ", Line/binary, $\n>>.
|
|
||||||
|
|
||||||
time_prefix() ->
|
time_prefix() ->
|
||||||
{{Y, M, D}, {H, I, S}} = calendar:local_time(),
|
{{Y, M, D}, {H, I, S}} = calendar:local_time(),
|
||||||
@ -133,9 +131,9 @@ time_prefix() ->
|
|||||||
-spec make_file() -> string().
|
-spec make_file() -> string().
|
||||||
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]),
|
Suffix = io_lib:format("~b~2..0b~2..0b-", [Year, Month, Day]),
|
||||||
RootDir = code:root_dir() ++ "/log/",
|
RootDir = code:root_dir() ++ "/log/",
|
||||||
lists:flatten(RootDir ++ Prefix ++ ?LOG_FILE).
|
lists:flatten(RootDir ++ ?LOG_FILE ++ "." ++ Suffix).
|
||||||
|
|
||||||
ensure_dir() ->
|
ensure_dir() ->
|
||||||
RootDir = code:root_dir() ++ "/log/",
|
RootDir = code:root_dir() ++ "/log/",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user