refactor(logger): stop using anonymous fun for logging

This commit is contained in:
Zaiming Shi 2021-07-31 18:33:31 +02:00
parent df69a9c031
commit 0f5c5db841
3 changed files with 27 additions and 7 deletions

View File

@ -14,10 +14,12 @@
%% limitations under the License.
%%--------------------------------------------------------------------
%% debug | info | notice | warning | error | critical | alert | emergency
-ifndef(EMQX_LOGGER_HRL).
-define(EMQX_LOGGER_HRL, true).
-compile({parse_transform, emqx_logger}).
%% debug | info | notice | warning | error | critical | alert | emergency
-define(DEBUG(Format), ?LOG(debug, Format, [])).
-define(DEBUG(Format, Args), ?LOG(debug, Format, Args)).
@ -41,10 +43,21 @@
-define(LOG(Level, Format), ?LOG(Level, Format, [])).
-define(LOG(Level, Format, Args),
begin
(logger:log(Level,#{},#{report_cb => fun(_) -> {'$logger_header'()++(Format), (Args)} end,
mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY},
line => ?LINE}))
-define(LOG(Level, Format, Args, Meta),
%% check 'allow' here so we do not have to pass an anonymous function
%% down to logger which may cause `badfun` exception during upgrade
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>>
, line => ?LINE
});
false ->
ok
end).
-define(LOG(Level, Format, Args), ?LOG(Level, Format, Args, #{})).
-endif.

View File

@ -197,12 +197,18 @@ json_obj(Data, Config) ->
json_kv(K, V, D, Config)
end, maps:new(), Data).
json_kv(mfa, {M, F, A}, Data, _Config) -> %% emqx/snabbkaffe
json_kv(mfa, {M, F, A}, Data, _Config) -> %% snabbkaffe
maps:put(mfa, <<(atom_to_binary(M, utf8))/binary, $:,
(atom_to_binary(F, utf8))/binary, $/,
(integer_to_binary(A))/binary>>, Data);
json_kv('$kind', Kind, Data, Config) -> %% snabbkaffe
maps:put(msg, json(Kind, Config), Data);
json_kv(gl, _, Data, _Config) ->
%% drop gl because it's not interesting
Data;
json_kv(file, _, Data, _Config) ->
%% drop 'file' because we have mfa
Data;
json_kv(K0, V, Data, Config) ->
K = json_key(K0),
case is_map(V) of

View File

@ -26,6 +26,7 @@
, 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).