fix: replace LOG by SLOG

This commit is contained in:
zhongwencool 2021-12-17 10:52:14 +08:00
parent 89d904b7ef
commit 38438415b8
3 changed files with 32 additions and 15 deletions

View File

@ -205,14 +205,14 @@ init([]) ->
{ok, #{timer => TRef, monitors => #{}, primary_log_level => OriginLogLevel}}. {ok, #{timer => TRef, monitors => #{}, primary_log_level => OriginLogLevel}}.
handle_call(Req, _From, State) -> handle_call(Req, _From, State) ->
?LOG(error, "Unexpected call: ~p", [Req]), ?SLOG(error, #{unexpected_call => Req}),
{reply, ok, State}. {reply, ok, State}.
handle_cast({delete_tag, Pid, Files}, State = #{monitors := Monitors}) -> handle_cast({delete_tag, Pid, Files}, State = #{monitors := Monitors}) ->
erlang:monitor(process, Pid), erlang:monitor(process, Pid),
{noreply, State#{monitors => Monitors#{Pid => Files}}}; {noreply, State#{monitors => Monitors#{Pid => Files}}};
handle_cast(Msg, State) -> handle_cast(Msg, State) ->
?LOG(error, "Unexpected cast: ~p", [Msg]), ?SLOG(error, #{unexpected_cast => Msg}),
{noreply, State}. {noreply, State}.
handle_info({'DOWN', _Ref, process, Pid, _Reason}, State = #{monitors := Monitors}) -> handle_info({'DOWN', _Ref, process, Pid, _Reason}, State = #{monitors := Monitors}) ->
@ -234,7 +234,7 @@ handle_info({mnesia_table_event, _Events}, State = #{timer := TRef}) ->
handle_info({timeout, TRef, update_trace}, State); handle_info({timeout, TRef, update_trace}, State);
handle_info(Info, State) -> handle_info(Info, State) ->
?LOG(error, "Unexpected info: ~p", [Info]), ?SLOG(error, #{unexpected_info => Info}),
{noreply, State}. {noreply, State}.
terminate(_Reason, #{timer := TRef, primary_log_level := OriginLogLevel}) -> terminate(_Reason, #{timer := TRef, primary_log_level := OriginLogLevel}) ->

View File

@ -107,10 +107,23 @@ group_trace_file(ZipDir, TraceLog, TraceFiles) ->
case Res of case Res of
{ok, Node, Bin} -> {ok, Node, Bin} ->
ZipName = ZipDir ++ Node ++ "-" ++ TraceLog, ZipName = ZipDir ++ Node ++ "-" ++ TraceLog,
ok = file:write_file(ZipName, Bin), case file:write_file(ZipName, Bin) of
[Node ++ "-" ++ TraceLog | Acc]; ok -> [Node ++ "-" ++ TraceLog | Acc];
Error ->
?SLOG(error, #{
msg => "write_file_failed",
error => Error,
zip_name => ZipName,
byte_size => byte_size(Bin)}),
Acc
end;
{error, Node, Reason} -> {error, Node, Reason} ->
?LOG(error, "download trace log error:~p", [{Node, TraceLog, Reason}]), ?SLOG(error, #{
msg => "download_trace_log_failed",
node => Node,
trace_log => TraceLog,
reason => Reason
}),
Acc Acc
end end
end, [], TraceFiles). end, [], TraceFiles).
@ -121,7 +134,12 @@ collect_trace_file(TraceLog) ->
cluster_call(Mod, Fun, Args, Timeout) -> cluster_call(Mod, Fun, Args, Timeout) ->
Nodes = mria_mnesia:running_nodes(), Nodes = mria_mnesia:running_nodes(),
{GoodRes, BadNodes} = rpc:multicall(Nodes, Mod, Fun, Args, Timeout), {GoodRes, BadNodes} = rpc:multicall(Nodes, Mod, Fun, Args, Timeout),
BadNodes =/= [] andalso ?LOG(error, "rpc call failed on ~p ~p", [BadNodes, {Mod, Fun, Args}]), BadNodes =/= [] andalso
?SLOG(error, #{
msg => "rpc_call_failed",
bad_nodes => BadNodes,
mfa => {Mod, Fun, Args }
}),
GoodRes. GoodRes.
stream_log_file(#{name := Name}, Params) -> stream_log_file(#{name := Name}, Params) ->

View File

@ -40,12 +40,12 @@ end_per_suite(_Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_common_test_helpers:stop_apps([]).
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
load(),
ok = emqx_trace:clear(), ok = emqx_trace:clear(),
reload(),
ct:pal("load:~p~n", [erlang:whereis(emqx_trace)]),
Config. Config.
end_per_testcase(_) -> end_per_testcase(_) ->
unload(),
ok. ok.
t_base_create_delete(_Config) -> t_base_create_delete(_Config) ->
@ -299,12 +299,13 @@ t_trace_file(_Config) ->
ok. ok.
t_download_log(_Config) -> t_download_log(_Config) ->
ClientId = <<"client-test">>, ClientId = <<"client-test-download">>,
Now = erlang:system_time(second), Now = erlang:system_time(second),
Start = to_rfc3339(Now), Start = to_rfc3339(Now),
Name = <<"test_client_id">>, Name = <<"test_client_id">>,
ok = emqx_trace:create([{<<"name">>, Name}, ok = emqx_trace:create([{<<"name">>, Name},
{<<"type">>, clientid}, {<<"clientid">>, ClientId}, {<<"start_at">>, Start}]), {<<"type">>, clientid}, {<<"clientid">>, ClientId}, {<<"start_at">>, Start}]),
ct:sleep(50),
{ok, Client} = emqtt:start_link([{clean_start, true}, {clientid, ClientId}]), {ok, Client} = emqtt:start_link([{clean_start, true}, {clientid, ClientId}]),
{ok, _} = emqtt:connect(Client), {ok, _} = emqtt:connect(Client),
[begin _ = emqtt:ping(Client) end ||_ <- lists:seq(1, 5)], [begin _ = emqtt:ping(Client) end ||_ <- lists:seq(1, 5)],
@ -341,8 +342,6 @@ t_find_closed_time(_Config) ->
to_rfc3339(Second) -> to_rfc3339(Second) ->
list_to_binary(calendar:system_time_to_rfc3339(Second)). list_to_binary(calendar:system_time_to_rfc3339(Second)).
load() -> reload() ->
emqx_trace:start_link(). catch ok = gen_server:stop(emqx_trace),
{ok, _Pid} = emqx_trace:start_link().
unload() ->
gen_server:stop(emqx_trace).