chore: refactor slog under emqx, conf and exhook

This commit is contained in:
Spycsh 2021-11-09 23:20:30 +01:00
parent 9cf8dfcb55
commit c89a663b53
6 changed files with 31 additions and 27 deletions

View File

@ -1053,7 +1053,7 @@ handle_info(clean_authz_cache, Channel) ->
handle_info(die_if_test = Info, Channel) ->
die_if_test_compiled(),
?LOG(error, "Unexpected info: ~p", [Info]),
?SLOG(error, #{msg => "unexpected_info", info => Info}),
{ok, Channel};
handle_info(Info, Channel) ->
@ -1133,7 +1133,7 @@ handle_timeout(_TRef, expire_quota_limit, Channel) ->
{ok, clean_timer(quota_timer, Channel)};
handle_timeout(_TRef, Msg, Channel) ->
?SLOG(error, #{msg => "unexpected_timeout", timeout_message => Msg}),
?SLOG(error, #{msg => "unexpected_timeout", timeout_msg => Msg}),
{ok, Channel}.
%%--------------------------------------------------------------------

View File

@ -215,7 +215,7 @@ handle_call({pending, SessionID, MarkerIDs}, _From, State) ->
Res = emqx_persistent_session:pending_messages_in_db(SessionID, MarkerIDs),
{reply, Res, State};
handle_call(Req, _From, State) ->
?LOG(error, "Unexpected call: ~p", [Req]),
?SLOG(error, #{msg => "unexpected_call", req => Req}),
{reply, ignored, State}.
handle_cast({delete_routes, SessionID, Subscriptions}, State) ->
@ -233,11 +233,11 @@ handle_cast({resume_end, SessionID, Pid}, State) ->
_ = emqx_session_router_worker_sup:abort_worker(Pid),
{noreply, State#{ pmon => Pmon }};
handle_cast(Msg, State) ->
?LOG(error, "Unexpected cast: ~p", [Msg]),
?SLOG(error, #{msg => "unexpected_cast", cast => Msg}),
{noreply, State}.
handle_info(Info, State) ->
?LOG(error, "Unexpected info: ~p", [Info]),
?SLOG(error, #{msg => "unexpected_info", info => Info}),
{noreply, State}.
terminate(_Reason, #{pool := Pool, id := Id}) ->

View File

@ -41,22 +41,22 @@ init([State]) ->
{ok, ensure_timer(State)}.
handle_call(Req, _From, State) ->
?LOG(error, "unexpected call: ~p", [Req]),
?SLOG(error, #{msg => "unexpected_call", call => Req}),
{reply, ignored, State}.
handle_cast(Msg, State) ->
?LOG(error, "unexpected msg: ~p", [Msg]),
?SLOG(error, #{msg => "unexpected_msg", cast => Msg}),
{noreply, State}.
handle_info({timeout, TRef, del_stale_mfa}, State = #{timer := TRef, max_history := MaxHistory}) ->
case mria:transaction(?CLUSTER_RPC_SHARD, fun del_stale_mfa/1, [MaxHistory]) of
{atomic, ok} -> ok;
Error -> ?LOG(error, "del_stale_cluster_rpc_mfa error:~p", [Error])
Error -> ?SLOG(error, #{msg => "del_stale_cluster_rpc_mfa_error", error => Error})
end,
{noreply, ensure_timer(State), hibernate};
handle_info(Info, State) ->
?LOG(error, "unexpected info: ~p", [Info]),
?SLOG(error, #{msg => "unexpected_info", info => Info}),
{noreply, State}.
terminate(_Reason, #{timer := TRef}) ->

View File

@ -304,7 +304,7 @@ merge_responsed_bool(Req, #{type := Type, value := {bool_result, NewBool}})
'STOP_AND_RETURN' -> {stop, NReq}
end;
merge_responsed_bool(_Req, Resp) ->
?LOG(warning, "Unknown responsed value ~0p to merge to callback chain", [Resp]),
?SLOG(warning, #{msg => "unknown_responsed_value", resp => Resp}),
ignore.
merge_responsed_message(_Req, #{type := 'IGNORE'}) ->
@ -316,5 +316,5 @@ merge_responsed_message(Req, #{type := Type, value := {message, NMessage}}) ->
'STOP_AND_RETURN' -> {stop, NReq}
end;
merge_responsed_message(_Req, Resp) ->
?LOG(warning, "Unknown responsed value ~0p to merge to callback chain", [Resp]),
?SLOG(warning, #{msg => "unknown_responsed_value", resp => Resp}),
ignore.

View File

@ -185,8 +185,9 @@ handle_info({timeout, _Ref, {reload, Name}}, State) ->
{error, not_found} ->
{noreply, NState};
{error, Reason} ->
?LOG(warning, "Failed to reload exhook callback server \"~ts\", "
"Reason: ~0p", [Name, Reason]),
?SLOG(warning, #{msg => "failed_to_reload_exhook_callback_server",
server_name => Name,
reason => Reason}),
{noreply, ensure_reload_timer(NState)}
end;
@ -230,8 +231,8 @@ do_load_server(Name, State0 = #state{
case emqx_exhook_server:load(Name, Options, ReqOpts) of
{ok, ServerState} ->
save(Name, ServerState),
?LOG(info, "Load exhook callback server "
"\"~ts\" successfully!", [Name]),
?SLOG(info, #{msg => "load_exhook_callback_server_successfully",
server_name => Name}),
{ok, State#state{
running = maps:put(Name, Options, Running),
waiting = maps:remove(Name, Waiting),

View File

@ -155,8 +155,10 @@ do_init(ChannName, ReqOpts) ->
try
{ok, resolve_hookspec(maps:get(hooks, InitialResp, []))}
catch _:Reason:Stk ->
?LOG(error, "try to init ~p failed, reason: ~p, stacktrace: ~0p",
[ChannName, Reason, Stk]),
?SLOG(error, #{msg => "failed_to_init_channel",
channel_name => ChannName,
reason => Reason,
stacktrace => Stk}),
{error, Reason}
end;
{error, Reason} ->
@ -194,7 +196,7 @@ ensure_hooks(HookSpecs) ->
lists:foreach(fun(Hookpoint) ->
case lists:keyfind(Hookpoint, 1, ?ENABLED_HOOKS) of
false ->
?LOG(error, "Unknown name ~ts to hook, skip it!", [Hookpoint]);
?SLOG(error, #{msg => "skipped_unknown_hookpoint", hookpoint => Hookpoint});
{Hookpoint, {M, F, A}} ->
emqx_hooks:put(Hookpoint, {M, F, A}),
ets:update_counter(?CNTER, Hookpoint, {2, 1}, {Hookpoint, 0})
@ -267,22 +269,23 @@ match_topic_filter(TopicName, TopicFilter) ->
-spec do_call(binary(), atom(), map(), map()) -> {ok, map()} | {error, term()}.
do_call(ChannName, Fun, Req, ReqOpts) ->
Options = ReqOpts#{channel => ChannName},
?LOG(debug, "Call ~0p:~0p(~0p, ~0p)", [?PB_CLIENT_MOD, Fun, Req, Options]),
?SLOG(debug, #{msg => "do_call", module => ?PB_CLIENT_MOD, function => Fun,
req => Req, options => Options}),
case catch apply(?PB_CLIENT_MOD, Fun, [Req, Options]) of
{ok, Resp, _Metadata} ->
?LOG(debug, "Response {ok, ~0p, ~0p}", [Resp, _Metadata]),
{ok, Resp, Metadata} ->
?SLOG(debug, #{msg => "do_call_ok", resp => Resp, metadata => Metadata}),
{ok, Resp};
{error, {Code, Msg}, _Metadata} ->
?LOG(error, "CALL ~0p:~0p(~0p, ~0p) response errcode: ~0p, errmsg: ~0p",
[?PB_CLIENT_MOD, Fun, Req, Options, Code, Msg]),
?SLOG(error, #{msg => "exhook_call_error", module => ?PB_CLIENT_MOD, function => Fun,
req => Req, options => Options, code => Code, packet => Msg}),
{error, {Code, Msg}};
{error, Reason} ->
?LOG(error, "CALL ~0p:~0p(~0p, ~0p) error: ~0p",
[?PB_CLIENT_MOD, Fun, Req, Options, Reason]),
?SLOG(error, #{msg => "exhook_call_error", module => ?PB_CLIENT_MOD, function => Fun,
req => Req, options => Options, reason => Reason}),
{error, Reason};
{'EXIT', {Reason, Stk}} ->
?LOG(error, "CALL ~0p:~0p(~0p, ~0p) throw an exception: ~0p, stacktrace: ~0p",
[?PB_CLIENT_MOD, Fun, Req, Options, Reason, Stk]),
?SLOG(error, #{msg => "exhook_call_exception", module => ?PB_CLIENT_MOD, function => Fun,
req => Req, options => Options, stacktrace => Stk}),
{error, Reason}
end.