Skip to content

Commit

Permalink
Merge pull request #9923 from zmstone/0206-fix-logger-formatter-crash
Browse files Browse the repository at this point in the history
fix(logger): fix REPORT_CB/2 CRASH logs
  • Loading branch information
zmstone committed Feb 7, 2023
2 parents 3587c4c + 4653ca6 commit 55889c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/emqx/src/emqx_logger_textfmt.erl
Expand Up @@ -23,7 +23,14 @@
check_config(X) -> logger_formatter:check_config(X).

format(#{msg := {report, ReportMap}, meta := Meta} = Event, Config) when is_map(ReportMap) ->
Report = enrich_report(ReportMap, Meta),
ReportList = enrich_report(ReportMap, Meta),
Report =
case is_list_report_acceptable(Meta) of
true ->
ReportList;
false ->
maps:from_list(ReportList)
end,
logger_formatter:format(Event#{msg := {report, Report}}, Config);
format(#{msg := {string, String}} = Event, Config) ->
format(Event#{msg => {"~ts ", [String]}}, Config);
Expand All @@ -34,6 +41,11 @@ format(#{msg := Msg0, meta := Meta} = Event, Config) ->
Msg3 = enrich_topic(Msg2, Meta),
logger_formatter:format(Event#{msg := Msg3}, Config).

is_list_report_acceptable(#{report_cb := Cb}) ->
Cb =:= fun logger:format_otp_report/1 orelse Cb =:= fun logger:format_report/1;
is_list_report_acceptable(_) ->
false.

enrich_report(ReportRaw, Meta) ->
%% clientid and peername always in emqx_conn's process metadata.
%% topic can be put in meta using ?SLOG/3, or put in msg's report by ?SLOG/2
Expand All @@ -47,6 +59,7 @@ enrich_report(ReportRaw, Meta) ->
MFA = maps:get(mfa, Meta, undefined),
Line = maps:get(line, Meta, undefined),
Msg = maps:get(msg, ReportRaw, undefined),
%% turn it into a list so that the order of the fields is determined
lists:foldl(
fun
({_, undefined}, Acc) -> Acc;
Expand Down
1 change: 1 addition & 0 deletions changes/v5.0.17/fix-9923.en.md
@@ -0,0 +1 @@
Fix REPORT_CB/2 CRASH error logs when erros happen during boot-up or shutdown.
1 change: 1 addition & 0 deletions changes/v5.0.17/fix-9923.zh.md
@@ -0,0 +1 @@
修复在启动和关闭过程中发生错误时,日志中的 REPORT_CB/2 CRASH 错误。

0 comments on commit 55889c9

Please sign in to comment.