refactor: delete mfa from log metadata

This commit is contained in:
Zaiming (Stone) Shi 2024-02-26 12:15:00 +01:00
parent de5e4491f7
commit 00c2aeb1ab
4 changed files with 5 additions and 42 deletions

View File

@ -30,10 +30,7 @@
logger:log( logger:log(
Level, Level,
(Data), (Data),
(Meta#{ Meta
mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY},
line => ?LINE
})
); );
false -> false ->
ok ok

View File

@ -272,7 +272,6 @@ json_obj_root(Data0, Config) ->
_ -> _ ->
json(Msg1, Config) json(Msg1, Config)
end, end,
Mfal = emqx_utils:format_mfal(Data0),
Data = Data =
maps:fold( maps:fold(
fun(K, V, D) -> fun(K, V, D) ->
@ -281,12 +280,12 @@ json_obj_root(Data0, Config) ->
end, end,
[], [],
maps:without( maps:without(
[time, gl, file, report_cb, msg, '$kind', mfa, level, line, is_trace], Data0 [time, gl, file, report_cb, msg, '$kind', level, is_trace], Data0
) )
), ),
lists:filter( lists:filter(
fun({_, V}) -> V =/= undefined end, fun({_, V}) -> V =/= undefined end,
[{time, Time}, {level, Level}, {msg, Msg}, {mfa, Mfal}] [{time, Time}, {level, Level}, {msg, Msg}]
) ++ Data. ) ++ Data.
json_obj(Data, Config) -> json_obj(Data, Config) ->

View File

@ -37,9 +37,8 @@ format(#{msg := {string, String}} = Event, Config) ->
%% trace %% trace
format(#{msg := Msg0, meta := Meta} = Event, Config) -> format(#{msg := Msg0, meta := Meta} = Event, Config) ->
Msg1 = enrich_client_info(Msg0, Meta), Msg1 = enrich_client_info(Msg0, Meta),
Msg2 = enrich_mfa(Msg1, Meta), Msg2 = enrich_topic(Msg1, Meta),
Msg3 = enrich_topic(Msg2, Meta), logger_formatter:format(Event#{msg := Msg2}, Config).
logger_formatter:format(Event#{msg := Msg3}, Config).
is_list_report_acceptable(#{report_cb := Cb}) -> is_list_report_acceptable(#{report_cb := Cb}) ->
Cb =:= fun logger:format_otp_report/1 orelse Cb =:= fun logger:format_report/1; Cb =:= fun logger:format_otp_report/1 orelse Cb =:= fun logger:format_report/1;
@ -61,7 +60,6 @@ enrich_report(ReportRaw, Meta) ->
end, end,
ClientId = maps:get(clientid, Meta, undefined), ClientId = maps:get(clientid, Meta, undefined),
Peer = maps:get(peername, Meta, undefined), Peer = maps:get(peername, Meta, undefined),
MFA = emqx_utils:format_mfal(Meta),
Msg = maps:get(msg, ReportRaw, undefined), Msg = maps:get(msg, ReportRaw, undefined),
%% turn it into a list so that the order of the fields is determined %% turn it into a list so that the order of the fields is determined
lists:foldl( lists:foldl(
@ -75,7 +73,6 @@ enrich_report(ReportRaw, Meta) ->
{topic, try_format_unicode(Topic)}, {topic, try_format_unicode(Topic)},
{clientid, try_format_unicode(ClientId)}, {clientid, try_format_unicode(ClientId)},
{peername, Peer}, {peername, Peer},
{mfa, try_format_unicode(MFA)},
{msg, Msg} {msg, Msg}
] ]
). ).
@ -99,11 +96,6 @@ try_format_unicode(Char) ->
_ -> List _ -> List
end. end.
enrich_mfa({Fmt, Args}, Data) when is_list(Fmt) ->
{Fmt ++ " mfa: ~ts", Args ++ [emqx_utils:format_mfal(Data)]};
enrich_mfa(Msg, _) ->
Msg.
enrich_client_info({Fmt, Args}, #{clientid := ClientId, peername := Peer}) when is_list(Fmt) -> enrich_client_info({Fmt, Args}, #{clientid := ClientId, peername := Peer}) when is_list(Fmt) ->
{" ~ts@~ts " ++ Fmt, [ClientId, Peer | Args]}; {" ~ts@~ts " ++ Fmt, [ClientId, Peer | Args]};
enrich_client_info({Fmt, Args}, #{clientid := ClientId}) when is_list(Fmt) -> enrich_client_info({Fmt, Args}, #{clientid := ClientId}) when is_list(Fmt) ->

View File

@ -65,7 +65,6 @@
flattermap/2, flattermap/2,
tcp_keepalive_opts/4, tcp_keepalive_opts/4,
format/1, format/1,
format_mfal/1,
call_first_defined/1, call_first_defined/1,
ntoa/1 ntoa/1
]). ]).
@ -553,30 +552,6 @@ tcp_keepalive_opts(OS, _Idle, _Interval, _Probes) ->
format(Term) -> format(Term) ->
iolist_to_binary(io_lib:format("~0p", [Term])). iolist_to_binary(io_lib:format("~0p", [Term])).
%% @doc Helper function for log formatters.
-spec format_mfal(map()) -> undefined | binary().
format_mfal(Data) ->
Line =
case maps:get(line, Data, undefined) of
undefined ->
<<"">>;
Num ->
["(", integer_to_list(Num), ")"]
end,
case maps:get(mfa, Data, undefined) of
{M, F, A} ->
iolist_to_binary([
atom_to_binary(M, utf8),
$:,
atom_to_binary(F, utf8),
$/,
integer_to_binary(A),
Line
]);
_ ->
undefined
end.
-spec call_first_defined(list({module(), atom(), list()})) -> term() | no_return(). -spec call_first_defined(list({module(), atom(), list()})) -> term() | no_return().
call_first_defined([{Module, Function, Args} | Rest]) -> call_first_defined([{Module, Function, Args} | Rest]) ->
try try