Просмотр исходного кода

refactor(emqx_plugin_libs_pool): structured logging

Zaiming (Stone) Shi 4 лет назад
Родитель
Сommit
6c99b64e4c

+ 11 - 5
apps/emqx_connector/src/emqx_connector_mongo.erl

@@ -34,6 +34,8 @@
         , on_jsonify/1
         ]).
 
+
+%% ecpool callback
 -export([connect/1]).
 
 -export([roots/0, fields/1]).
@@ -125,7 +127,7 @@ on_start(InstId, Config = #{mongo_type := Type,
             {options, init_topology_options(maps:to_list(Topology), [])},
             {worker_options, init_worker_options(maps:to_list(NConfig), SslOpts)}],
     PoolName = emqx_plugin_libs_pool:pool_name(InstId),
-    _ = emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Opts),
+    ok = emqx_plugin_libs_pool:start_pool(PoolName, ?MODULE, Opts),
     {ok, #{poolname => PoolName, type => Type}}.
 
 on_stop(InstId, #{poolname := PoolName}) ->
@@ -177,18 +179,22 @@ health_check(PoolName) ->
 
 %% ===================================================================
 
-check_worker_health(Worker) -> 
+%% TODO: log reasons
+check_worker_health(Worker) ->
     case ecpool_worker:client(Worker) of
         {ok, Conn} ->
             %% we don't care if this returns something or not, we just to test the connection
             try mongo_api:find_one(Conn, <<"foo">>, #{}, #{}) of
-                {error, _} -> false;
+                {error, _Reason} ->
+                    false;
                 _ ->
                     true
             catch
-                _Class:_Error -> false
+                _ : _ ->
+                    false
             end;
-        _ -> false
+        _ ->
+            false
     end.
 
 connect(Opts) ->

+ 12 - 5
apps/emqx_plugin_libs/src/emqx_plugin_libs_pool.erl

@@ -22,26 +22,33 @@
         , health_check/3
         ]).
 
+-include_lib("emqx/include/logger.hrl").
+
 pool_name(ID) when is_binary(ID) ->
     list_to_atom(binary_to_list(ID)).
 
 start_pool(Name, Mod, Options) ->
     case ecpool:start_sup_pool(Name, Mod, Options) of
-        {ok, _} -> logger:log(info, "Initiated ~0p Successfully", [Name]);
+        {ok, _} ->
+            ?SLOG(info, #{msg => "start_ecpool_ok", pool_name => Name});
         {error, {already_started, _Pid}} ->
             stop_pool(Name),
             start_pool(Name, Mod, Options);
         {error, Reason} ->
-            logger:log(error, "Initiate ~0p failed ~0p", [Name, Reason]),
+            ?SLOG(error, #{msg => "start_ecpool_error", pool_name => Name,
+                           reason => Reason}),
             error({start_pool_failed, Name})
     end.
 
 stop_pool(Name) ->
     case ecpool:stop_sup_pool(Name) of
-        ok -> logger:log(info, "Destroyed ~0p Successfully", [Name]);
-        {error, not_found} -> ok;
+        ok ->
+            ?SLOG(info, #{msg => "stop_ecpool_ok", pool_name => Name});
+        {error, not_found} ->
+            ok;
         {error, Reason} ->
-            logger:log(error, "Destroy ~0p failed, ~0p", [Name, Reason]),
+            ?SLOG(error, #{msg => "stop_ecpool_failed", pool_name => Name,
+                           reason => Reason}),
             error({stop_pool_failed, Name})
     end.