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} |
|
||||
{stop, Reason :: term(), NewState :: #state{}}).
|
||||
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(),
|
||||
case FilePath =:= OldFilePath of
|
||||
true ->
|
||||
@ -83,7 +84,8 @@ handle_cast({write, Data}, State = #state{file_path = OldFilePath, file = OldFil
|
||||
false ->
|
||||
file:close(OldFile),
|
||||
{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.
|
||||
|
||||
%% @private
|
||||
@ -118,13 +120,9 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
||||
%%%===================================================================
|
||||
|
||||
format(Data) when is_binary(Data) ->
|
||||
TimePrefix = time_prefix(),
|
||||
NData = iolist_to_binary(Data),
|
||||
<<TimePrefix/binary, " ", NData/binary, $\n>>;
|
||||
iolist_to_binary(Data);
|
||||
format(Items) when is_list(Items) ->
|
||||
TimePrefix = time_prefix(),
|
||||
Line = iolist_to_binary(lists:join(<<"\t">>, Items)),
|
||||
<<TimePrefix/binary, " ", Line/binary, $\n>>.
|
||||
iolist_to_binary(lists:join(<<"\t">>, Items)).
|
||||
|
||||
time_prefix() ->
|
||||
{{Y, M, D}, {H, I, S}} = calendar:local_time(),
|
||||
@ -133,9 +131,9 @@ time_prefix() ->
|
||||
-spec make_file() -> string().
|
||||
make_file() ->
|
||||
{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/",
|
||||
lists:flatten(RootDir ++ Prefix ++ ?LOG_FILE).
|
||||
lists:flatten(RootDir ++ ?LOG_FILE ++ "." ++ Suffix).
|
||||
|
||||
ensure_dir() ->
|
||||
RootDir = code:root_dir() ++ "/log/",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user