cth_log_redirect_any_domains: common_test hook to handle domain-specific log messages

common_test installs its own logger handler, which is great.
Unfortunately, this logger handler drops all messages having a domain,
except when the domain is among the domains used by Erlang itself.

In RabbitMQ, we use logger domains to categorize messages. Therefore
those messages are dropped by the common_test's logger handler.

This commit introduces another logger handler which sits on top of the
common_test one and makes sure messages with a domain are logged as
well.
This commit is contained in:
Jean-Sébastien Pédron 2022-01-26 16:02:04 +01:00
parent bcb8733880
commit cefb6aaef5
No known key found for this signature in database
GPG Key ID: 39E99761A5FD94CC
3 changed files with 55 additions and 0 deletions

2
.gitignore vendored
View File

@ -21,6 +21,8 @@
!/deps/rabbitmq_cli/
!/deps/rabbitmq_codegen/
!/deps/rabbitmq_consistent_hash_exchange/
!/deps/rabbitmq_ct_helpers/
!/deps/rabbitmq_ct_client_helpers/
!/deps/rabbitmq_event_exchange/
!/deps/rabbitmq_federation/
!/deps/rabbitmq_federation_management/

View File

@ -0,0 +1,23 @@
-module(cth_log_redirect_any_domains).
-export([log/2]).
-define(BACKEND_MODULE, cth_log_redirect).
%% Reversed behavior compared to `cth_log_redirect': log events with an
%% unknown domain are sent to the `cth_log_redirect' server, others are
%% dropped (as they are already handled by `cth_log_redirect').
log(#{msg:={report,_Msg},meta:=#{domain:=[otp,sasl]}},_Config) ->
ok;
log(#{meta:=#{domain:=[otp]}},_Config) ->
ok;
log(#{meta:=#{domain:=_}}=Log,Config) ->
do_log(add_log_category(Log,error_logger),Config);
log(_Log,_Config) ->
ok.
add_log_category(#{meta:=Meta}=Log,Category) ->
Log#{meta=>Meta#{?BACKEND_MODULE=>#{category=>Category}}}.
do_log(Log,Config) ->
gen_server:call(?BACKEND_MODULE,{log,Log,Config}).

View File

@ -7,6 +7,7 @@
-module(rabbit_ct_helpers).
-include_lib("kernel/include/logger.hrl").
-include_lib("common_test/include/ct.hrl").
-deprecated({is_mixed_versions,1,"Use is_mixed_versions/0 instead"}).
@ -26,6 +27,7 @@
load_rabbitmqctl_app/1,
ensure_rabbitmq_plugins_cmd/1,
ensure_rabbitmq_queues_cmd/1,
redirect_logger_to_ct_logs/1,
init_skip_as_error_flag/1,
start_long_running_testsuite_monitor/1,
stop_long_running_testsuite_monitor/1,
@ -155,6 +157,34 @@ run_steps(Config, [Step | Rest]) ->
run_steps(Config, []) ->
Config.
redirect_logger_to_ct_logs(Config) ->
ct:pal(
?LOW_IMPORTANCE,
"Configuring logger to send logs to common_test logs"),
logger:set_handler_config(cth_log_redirect, level, debug),
%% Let's use the same format as RabbitMQ itself.
logger:set_handler_config(
cth_log_redirect, formatter,
rabbit_prelaunch_early_logging:default_file_formatter(#{})),
%% We use an addition logger handler for messages tagged with a non-OTP
%% domain because by default, `cth_log_redirect' drop them.
{ok, LogCfg0} = logger:get_handler_config(cth_log_redirect),
LogCfg = maps:remove(id, maps:remove(module, LogCfg0)),
ok = logger:add_handler(
cth_log_redirect_any_domains, cth_log_redirect_any_domains,
LogCfg),
logger:remove_handler(default),
ct:pal(
?LOW_IMPORTANCE,
"Logger configured to send logs to common_test logs; you should see "
"a message below saying so"),
?LOG_INFO("Logger message logged to common_test logs"),
Config.
init_skip_as_error_flag(Config) ->
SkipAsError = case os:getenv("RABBITMQ_CT_SKIP_AS_ERROR") of
false -> false;