chore(audit): add from field

This commit is contained in:
JianBo He 2023-09-27 20:10:30 +08:00
parent ece7d5b52a
commit dd9938114c
7 changed files with 17 additions and 10 deletions

View File

@ -61,7 +61,9 @@
)
end).
-define(AUDIT(_Level_, _Msg_, _Meta_), begin
-define(AUDIT(_Level_, _From_, _Meta_), ?AUDIT(_Level_, _From_, undefined, _Meta_)).
-define(AUDIT(_Level_, _From_, _Msg_, _Meta_), begin
case emqx_config:get([log, audit], #{enable => false}) of
#{enable := false} ->
ok;
@ -72,7 +74,7 @@ end).
_Level_,
[{emqx_audit, fun(L, _) -> L end, undefined, undefined}],
{report, _Msg_},
_Meta_
_Meta_#{from => _From_}
);
gt ->
ok

View File

@ -87,8 +87,13 @@ log(List, Msg, Meta) ->
log(debug, List, Msg, Meta).
log(Level, List, Msg, Meta) ->
Log = #{level => Level, meta => enrich_meta(Meta), msg => Msg},
log_filter(List, Log).
Log = #{level => Level, meta => enrich_meta(Meta)},
Log1 =
case Msg of
undefined -> Log;
_ -> maps:put(msg, Msg, Log)
end,
log_filter(List, Log1).
enrich_meta(Meta) ->
case logger:get_process_metadata() of

View File

@ -145,7 +145,7 @@ run_command(Cmd, Args) when is_atom(Cmd) ->
audit_log(
audit_level(Result, Duration),
"from_cli",
cli,
#{duration_ms => Duration, cmd => Cmd, args => Args, node => node()}
),
Result.

View File

@ -30,7 +30,7 @@ log(Meta0) ->
Meta = emqx_utils:redact(Meta2),
?AUDIT(
Level,
"from_api",
rest_api,
Meta#{
from => from(maps:get(auth_type, Meta0, "")),
username => binary_to_list(Username),

View File

@ -47,7 +47,7 @@ post_boot() ->
ok = ensure_apps_started(),
ok = print_vsn(),
ok = start_autocluster(),
?AUDIT(alert, "from_cli", #{time => logger:timestamp(), event => "emqx_start"}),
?AUDIT(alert, cli, #{time => logger:timestamp(), event => "emqx_start"}),
ignore.
-ifdef(TEST).

View File

@ -67,8 +67,8 @@ graceful() ->
%% @doc Shutdown the Erlang VM and wait indefinitely.
graceful_wait() ->
?AUDIT(alert, "from_cli", #{
time => logger:timestamp(), msg => "run_emqx_stop_to_grace_shutdown"
?AUDIT(alert, cli, run_emqx_stop_to_grace_shutdown, #{
time => logger:timestamp()
}),
ok = graceful(),
exit_loop().

View File

@ -112,7 +112,7 @@ max_heap_size_warning(MF, Args) ->
log(_, {?MODULE, prompt_func}, [[{history, _}]]) ->
ok;
log(IsAllow, MF, Args) ->
?AUDIT(warning, "from_remote_console", #{
?AUDIT(warning, remote_console, #{
time => logger:timestamp(),
function => MF,
args => pp_args(Args),