Merge pull request #12113 from zhongwencool/log-fix

fix: audit log format
This commit is contained in:
zhongwencool 2023-12-07 09:56:01 +08:00 committed by GitHub
commit 850c6027d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 9 deletions

View File

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

View File

@ -1427,7 +1427,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;

View File

@ -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, [

View File

@ -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.

View File

@ -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), http_status_code => Code,
operation_result => operation_result(Meta), 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(Code, _) when Code >= 300 -> failure;
operation_result(_) -> success. 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;