ソースを参照

fix: audit log format

zhongwencool 2 年 前
コミット
8965aa2a21

+ 2 - 2
apps/emqx_audit/src/emqx_audit.erl

@@ -95,7 +95,7 @@ to_audit(#{from := erlang_console, function := F, args := Args}) ->
         http_method = <<"">>,
         http_method = <<"">>,
         http_request = <<"">>,
         http_request = <<"">>,
         duration_ms = 0,
         duration_ms = 0,
-        args = iolist_to_binary(io_lib:format("~p: ~p~n", [F, Args]))
+        args = iolist_to_binary(io_lib:format("~p: ~ts", [F, Args]))
     }.
     }.
 
 
 log(_Level, undefined, _Handler) ->
 log(_Level, undefined, _Handler) ->
@@ -141,7 +141,7 @@ handle_continue(setup, State) ->
     NewState = State#{role => mria_rlog:role()},
     NewState = State#{role => mria_rlog:role()},
     ?AUDIT(alert, #{
     ?AUDIT(alert, #{
         cmd => emqx,
         cmd => emqx,
-        args => ["start"],
+        args => [<<"start">>],
         version => emqx_release:version(),
         version => emqx_release:version(),
         from => cli,
         from => cli,
         duration_ms => 0
         duration_ms => 0

+ 2 - 1
apps/emqx_conf/src/emqx_conf_schema.erl

@@ -1520,7 +1520,8 @@ ensure_file_handlers(Conf, _Opts) ->
 
 
 convert_rotation(undefined, _Opts) -> undefined;
 convert_rotation(undefined, _Opts) -> undefined;
 convert_rotation(#{} = Rotation, _Opts) -> maps:get(<<"count">>, Rotation, 10);
 convert_rotation(#{} = Rotation, _Opts) -> maps:get(<<"count">>, Rotation, 10);
-convert_rotation(Count, _Opts) when is_integer(Count) -> Count.
+convert_rotation(Count, _Opts) when is_integer(Count) -> Count;
+convert_rotation(Count, _Opts) -> throw({"bad_rotation", Count}).
 
 
 ensure_unicode_path(undefined, _) ->
 ensure_unicode_path(undefined, _) ->
     undefined;
     undefined;

+ 1 - 1
apps/emqx_ctl/src/emqx_ctl.app.src

@@ -1,6 +1,6 @@
 {application, emqx_ctl, [
 {application, emqx_ctl, [
     {description, "Backend for emqx_ctl script"},
     {description, "Backend for emqx_ctl script"},
-    {vsn, "0.1.5"},
+    {vsn, "0.1.6"},
     {registered, []},
     {registered, []},
     {mod, {emqx_ctl_app, []}},
     {mod, {emqx_ctl_app, []}},
     {applications, [
     {applications, [

+ 15 - 1
apps/emqx_ctl/src/emqx_ctl.erl

@@ -339,11 +339,25 @@ audit_log(Level, From, Log) ->
             try
             try
                 apply(Mod, Fun, [Level, From, normalize_audit_log_args(Log)])
                 apply(Mod, Fun, [Level, From, normalize_audit_log_args(Log)])
             catch
             catch
+                _:{aborted, {no_exists, emqx_audit}} ->
+                    case Log of
+                        #{cmd := cluster, args := ["leave"]} ->
+                            ok;
+                        _ ->
+                            ?LOG_ERROR(#{
+                                msg => "ctl_command_crashed",
+                                reason => "emqx_audit table not found",
+                                log => normalize_audit_log_args(Log),
+                                from => From
+                            })
+                    end;
                 _:Reason:Stacktrace ->
                 _:Reason:Stacktrace ->
                     ?LOG_ERROR(#{
                     ?LOG_ERROR(#{
                         msg => "ctl_command_crashed",
                         msg => "ctl_command_crashed",
                         stacktrace => Stacktrace,
                         stacktrace => Stacktrace,
-                        reason => Reason
+                        reason => Reason,
+                        log => normalize_audit_log_args(Log),
+                        from => From
                     })
                     })
             end
             end
     end.
     end.

+ 6 - 4
apps/emqx_dashboard/src/emqx_dashboard_audit.erl

@@ -46,6 +46,7 @@ log_meta(Meta, Req) ->
         true ->
         true ->
             undefined;
             undefined;
         false ->
         false ->
+            Code = maps:get(code, Meta),
             Meta1 = #{
             Meta1 = #{
                 time => logger:timestamp(),
                 time => logger:timestamp(),
                 from => from(Meta),
                 from => from(Meta),
@@ -56,8 +57,8 @@ log_meta(Meta, Req) ->
                 %% method for http filter api.
                 %% method for http filter api.
                 http_method => Method,
                 http_method => Method,
                 http_request => http_request(Meta),
                 http_request => http_request(Meta),
-                http_status_code => maps:get(code, Meta),
-                operation_result => operation_result(Meta),
+                http_status_code => Code,
+                operation_result => operation_result(Code, Meta),
                 node => node()
                 node => node()
             },
             },
             Meta2 = maps:without([req_start, req_end, method, headers, body, bindings, code], Meta),
             Meta2 = maps:without([req_start, req_end, method, headers, body, bindings, code], Meta),
@@ -105,8 +106,9 @@ operation_type(Meta) ->
 http_request(Meta) ->
 http_request(Meta) ->
     maps:with([method, headers, bindings, body], Meta).
     maps:with([method, headers, bindings, body], Meta).
 
 
-operation_result(#{failure := _}) -> failure;
-operation_result(_) -> success.
+operation_result(Code, _) when Code >= 300 -> failure;
+operation_result(_, #{failure := _}) -> failure;
+operation_result(_, _) -> success.
 
 
 level(get, _Code) -> debug;
 level(get, _Code) -> debug;
 level(_, Code) when Code >= 200 andalso Code < 300 -> info;
 level(_, Code) when Code >= 200 andalso Code < 300 -> info;