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

refactor(log/textfmt): print mfa metadata in text formatter

Zaiming Shi 4 лет назад
Родитель
Сommit
683974e1c8

+ 6 - 3
apps/emqx/include/logger.hrl

@@ -47,9 +47,7 @@
         case logger:allow(Level, ?MODULE) of
             true ->
                 logger:log(Level, (Format), (Args),
-                           (Meta)#{ mfa => <<(atom_to_binary(?MODULE, utf8))/binary, $:,
-                                             (atom_to_binary(?FUNCTION_NAME, utf8))/binary, $/,
-                                             (integer_to_binary(?FUNCTION_ARITY))/binary>>
+                           (Meta)#{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
                                   , line => ?LINE
                                   });
             false ->
@@ -58,4 +56,9 @@
 
 -define(LOG(Level, Format, Args), ?LOG(Level, Format, Args, #{})).
 
+%% structured logging
+-define(SLOG(Level, Data),
+        logger:log(Level, Data, #{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
+                                 , line => ?LINE})).
+
 -endif.

+ 1 - 1
apps/emqx/src/emqx_logger_jsonfmt.erl

@@ -219,7 +219,7 @@ json_obj(Data, Config) ->
                       json_kv(K, V, D, Config)
               end, maps:new(), Data).
 
-json_kv(mfa, {M, F, A}, Data, _Config) -> %% snabbkaffe
+json_kv(mfa, {M, F, A}, Data, _Config) ->
     maps:put(mfa, <<(atom_to_binary(M, utf8))/binary, $:,
                     (atom_to_binary(F, utf8))/binary, $/,
                     (integer_to_binary(A))/binary>>, Data);

+ 17 - 19
apps/emqx/src/emqx_logger_textfmt.erl

@@ -19,26 +19,24 @@
 -export([format/2]).
 -export([check_config/1]).
 
-%% metadata fields which we do not wish to merge into log data
--define(WITHOUT_MERGE,
-        [ report_cb % just a callback
-        , time      % formatted as a part of templated message
-        , peername  % formatted as a part of templated message
-        , clientid  % formatted as a part of templated message
-        , gl        % not interesting
-        , file
-        ]).
-
 check_config(X) -> logger_formatter:check_config(X).
 
-format(#{msg := Msg0, meta := Meta} = Event, Config) ->
-    Msg = maybe_merge(Msg0, Meta),
-    logger_formatter:format(Event#{msg := Msg}, Config).
+format(#{msg := {report, Report}, meta := Meta} = Event, Config) when is_map(Report) ->
+    logger_formatter:format(Event#{msg := {report, enrich(Report, Meta)}}, Config);
+format(#{msg := {Fmt, Args}, meta := Meta} = Event, Config) when is_list(Fmt) ->
+    {NewFmt, NewArgs} = enrich_fmt(Fmt, Args, Meta),
+    logger_formatter:format(Event#{msg := {NewFmt, NewArgs}}, Config).
+
+enrich(Report, #{mfa := Mfa, line := Line}) ->
+    Report#{mfa => mfa(Mfa), line => Line};
+enrich(Report, _) -> Report.
 
-maybe_merge({report, Report}, Meta) when is_map(Report) ->
-    {report, maps:merge(Report, filter(Meta))};
-maybe_merge(Report, _Meta) ->
-    Report.
+enrich_fmt(Fmt, Args, #{mfa := Mfa, line := Line}) ->
+    {Fmt ++ " mfa: ~s line: ~w", Args ++ [mfa(Mfa), Line]};
+enrich_fmt(Fmt, Args, _) ->
+    {Fmt, Args}.
 
-filter(Meta) ->
-    maps:without(?WITHOUT_MERGE, Meta).
+mfa({M, F, A}) ->
+    <<(atom_to_binary(M, utf8))/binary, $:,
+      (atom_to_binary(F, utf8))/binary, $/,
+      (integer_to_binary(A))/binary>>.