Преглед изворни кода

Merge pull request #6107 from Spycsh/slog

chore: refactor slog under emqx, conf and exhook
Spycsh пре 4 година
родитељ
комит
fda2e86187

+ 2 - 2
apps/emqx/src/emqx_channel.erl

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

+ 3 - 3
apps/emqx/src/emqx_session_router.erl

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

+ 4 - 4
apps/emqx_conf/src/emqx_cluster_rpc_handler.erl

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

+ 2 - 2
apps/emqx_exhook/src/emqx_exhook_handler.erl

@@ -304,7 +304,7 @@ merge_responsed_bool(Req, #{type := Type, value := {bool_result, NewBool}})
         'STOP_AND_RETURN' -> {stop, NReq}
         'STOP_AND_RETURN' -> {stop, NReq}
     end;
     end;
 merge_responsed_bool(_Req, Resp) ->
 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.
     ignore.
 
 
 merge_responsed_message(_Req, #{type := '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}
         'STOP_AND_RETURN' -> {stop, NReq}
     end;
     end;
 merge_responsed_message(_Req, Resp) ->
 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.
     ignore.

+ 5 - 4
apps/emqx_exhook/src/emqx_exhook_mngr.erl

@@ -185,8 +185,9 @@ handle_info({timeout, _Ref, {reload, Name}}, State) ->
         {error, not_found} ->
         {error, not_found} ->
             {noreply, NState};
             {noreply, NState};
         {error, Reason} ->
         {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)}
             {noreply, ensure_reload_timer(NState)}
     end;
     end;
 
 
@@ -230,8 +231,8 @@ do_load_server(Name, State0 = #state{
                     case emqx_exhook_server:load(Name, Options, ReqOpts) of
                     case emqx_exhook_server:load(Name, Options, ReqOpts) of
                         {ok, ServerState} ->
                         {ok, ServerState} ->
                             save(Name, 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{
                             {ok, State#state{
                                    running = maps:put(Name, Options, Running),
                                    running = maps:put(Name, Options, Running),
                                    waiting = maps:remove(Name, Waiting),
                                    waiting = maps:remove(Name, Waiting),

+ 15 - 12
apps/emqx_exhook/src/emqx_exhook_server.erl

@@ -155,8 +155,10 @@ do_init(ChannName, ReqOpts) ->
             try
             try
                 {ok, resolve_hookspec(maps:get(hooks, InitialResp, []))}
                 {ok, resolve_hookspec(maps:get(hooks, InitialResp, []))}
             catch _:Reason:Stk ->
             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}
                 {error, Reason}
             end;
             end;
         {error, Reason} ->
         {error, Reason} ->
@@ -194,7 +196,7 @@ ensure_hooks(HookSpecs) ->
     lists:foreach(fun(Hookpoint) ->
     lists:foreach(fun(Hookpoint) ->
         case lists:keyfind(Hookpoint, 1, ?ENABLED_HOOKS) of
         case lists:keyfind(Hookpoint, 1, ?ENABLED_HOOKS) of
             false ->
             false ->
-                ?LOG(error, "Unknown name ~ts to hook, skip it!", [Hookpoint]);
+                ?SLOG(error, #{msg => "skipped_unknown_hookpoint", hookpoint => Hookpoint});
             {Hookpoint, {M, F, A}} ->
             {Hookpoint, {M, F, A}} ->
                 emqx_hooks:put(Hookpoint, {M, F, A}),
                 emqx_hooks:put(Hookpoint, {M, F, A}),
                 ets:update_counter(?CNTER, Hookpoint, {2, 1}, {Hookpoint, 0})
                 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()}.
 -spec do_call(binary(), atom(), map(), map()) -> {ok, map()} | {error, term()}.
 do_call(ChannName, Fun, Req, ReqOpts) ->
 do_call(ChannName, Fun, Req, ReqOpts) ->
     Options = ReqOpts#{channel => ChannName},
     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
     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};
             {ok, Resp};
         {error, {Code, Msg}, _Metadata} ->
         {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, {Code, Msg}};
         {error, Reason} ->
         {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};
             {error, Reason};
         {'EXIT', {Reason, Stk}} ->
         {'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}
             {error, Reason}
     end.
     end.