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

This commit is contained in:
Zaiming Shi 2021-07-31 21:25:00 +02:00
parent 2998a87a8d
commit 683974e1c8
3 changed files with 24 additions and 23 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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).
maybe_merge({report, Report}, Meta) when is_map(Report) ->
{report, maps:merge(Report, filter(Meta))};
maybe_merge(Report, _Meta) ->
Report.
enrich(Report, #{mfa := Mfa, line := Line}) ->
Report#{mfa => mfa(Mfa), line => Line};
enrich(Report, _) -> Report.
filter(Meta) ->
maps:without(?WITHOUT_MERGE, Meta).
enrich_fmt(Fmt, Args, #{mfa := Mfa, line := Line}) ->
{Fmt ++ " mfa: ~s line: ~w", Args ++ [mfa(Mfa), Line]};
enrich_fmt(Fmt, Args, _) ->
{Fmt, Args}.
mfa({M, F, A}) ->
<<(atom_to_binary(M, utf8))/binary, $:,
(atom_to_binary(F, utf8))/binary, $/,
(integer_to_binary(A))/binary>>.