From 8965aa2a21d598aba59acb8e4ed531f7679ff8a1 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Wed, 6 Dec 2023 16:58:04 +0800 Subject: [PATCH] fix: audit log format --- apps/emqx_audit/src/emqx_audit.erl | 4 ++-- apps/emqx_conf/src/emqx_conf_schema.erl | 3 ++- apps/emqx_ctl/src/emqx_ctl.app.src | 2 +- apps/emqx_ctl/src/emqx_ctl.erl | 16 +++++++++++++++- apps/emqx_dashboard/src/emqx_dashboard_audit.erl | 10 ++++++---- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/emqx_audit/src/emqx_audit.erl b/apps/emqx_audit/src/emqx_audit.erl index 98f4a70e8..ffbfaebb3 100644 --- a/apps/emqx_audit/src/emqx_audit.erl +++ b/apps/emqx_audit/src/emqx_audit.erl @@ -95,7 +95,7 @@ to_audit(#{from := erlang_console, function := F, args := Args}) -> http_method = <<"">>, http_request = <<"">>, 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) -> @@ -141,7 +141,7 @@ handle_continue(setup, State) -> NewState = State#{role => mria_rlog:role()}, ?AUDIT(alert, #{ cmd => emqx, - args => ["start"], + args => [<<"start">>], version => emqx_release:version(), from => cli, duration_ms => 0 diff --git a/apps/emqx_conf/src/emqx_conf_schema.erl b/apps/emqx_conf/src/emqx_conf_schema.erl index 0db3c4a45..a0f7f5099 100644 --- a/apps/emqx_conf/src/emqx_conf_schema.erl +++ b/apps/emqx_conf/src/emqx_conf_schema.erl @@ -1520,7 +1520,8 @@ ensure_file_handlers(Conf, _Opts) -> convert_rotation(undefined, _Opts) -> undefined; 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, _) -> undefined; diff --git a/apps/emqx_ctl/src/emqx_ctl.app.src b/apps/emqx_ctl/src/emqx_ctl.app.src index 8abf67eb9..29fe06ccc 100644 --- a/apps/emqx_ctl/src/emqx_ctl.app.src +++ b/apps/emqx_ctl/src/emqx_ctl.app.src @@ -1,6 +1,6 @@ {application, emqx_ctl, [ {description, "Backend for emqx_ctl script"}, - {vsn, "0.1.5"}, + {vsn, "0.1.6"}, {registered, []}, {mod, {emqx_ctl_app, []}}, {applications, [ diff --git a/apps/emqx_ctl/src/emqx_ctl.erl b/apps/emqx_ctl/src/emqx_ctl.erl index 8f6b3309b..60413fb83 100644 --- a/apps/emqx_ctl/src/emqx_ctl.erl +++ b/apps/emqx_ctl/src/emqx_ctl.erl @@ -339,11 +339,25 @@ audit_log(Level, From, Log) -> try apply(Mod, Fun, [Level, From, normalize_audit_log_args(Log)]) 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 -> ?LOG_ERROR(#{ msg => "ctl_command_crashed", stacktrace => Stacktrace, - reason => Reason + reason => Reason, + log => normalize_audit_log_args(Log), + from => From }) end end. diff --git a/apps/emqx_dashboard/src/emqx_dashboard_audit.erl b/apps/emqx_dashboard/src/emqx_dashboard_audit.erl index 4b51b2cb0..957d5d0bb 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_audit.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_audit.erl @@ -46,6 +46,7 @@ log_meta(Meta, Req) -> true -> undefined; false -> + Code = maps:get(code, Meta), Meta1 = #{ time => logger:timestamp(), from => from(Meta), @@ -56,8 +57,8 @@ log_meta(Meta, Req) -> %% method for http filter api. http_method => Method, 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() }, Meta2 = maps:without([req_start, req_end, method, headers, body, bindings, code], Meta), @@ -105,8 +106,9 @@ operation_type(Meta) -> http_request(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(_, Code) when Code >= 200 andalso Code < 300 -> info;