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

View File

@ -219,7 +219,7 @@ json_obj(Data, Config) ->
json_kv(K, V, D, Config) json_kv(K, V, D, Config)
end, maps:new(), Data). 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, $:, maps:put(mfa, <<(atom_to_binary(M, utf8))/binary, $:,
(atom_to_binary(F, utf8))/binary, $/, (atom_to_binary(F, utf8))/binary, $/,
(integer_to_binary(A))/binary>>, Data); (integer_to_binary(A))/binary>>, Data);

View File

@ -19,26 +19,24 @@
-export([format/2]). -export([format/2]).
-export([check_config/1]). -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). check_config(X) -> logger_formatter:check_config(X).
format(#{msg := Msg0, meta := Meta} = Event, Config) -> format(#{msg := {report, Report}, meta := Meta} = Event, Config) when is_map(Report) ->
Msg = maybe_merge(Msg0, Meta), logger_formatter:format(Event#{msg := {report, enrich(Report, Meta)}}, Config);
logger_formatter:format(Event#{msg := Msg}, 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) -> enrich(Report, #{mfa := Mfa, line := Line}) ->
{report, maps:merge(Report, filter(Meta))}; Report#{mfa => mfa(Mfa), line => Line};
maybe_merge(Report, _Meta) -> enrich(Report, _) -> Report.
Report.
filter(Meta) -> enrich_fmt(Fmt, Args, #{mfa := Mfa, line := Line}) ->
maps:without(?WITHOUT_MERGE, Meta). {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>>.