ソースを参照

Merge pull request #9923 from zmstone/0206-fix-logger-formatter-crash

fix(logger): fix REPORT_CB/2 CRASH logs
Zaiming (Stone) Shi 3 年 前
コミット
55889c9d39

+ 14 - 1
apps/emqx/src/emqx_logger_textfmt.erl

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

+ 1 - 0
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 - 0
changes/v5.0.17/fix-9923.zh.md

@@ -0,0 +1 @@
+修复在启动和关闭过程中发生错误时,日志中的 REPORT_CB/2 CRASH 错误。