Skip to content

Commit 81ff300

Browse files
Gracefully handle cases when logging exchange does not exist
Client test suites that test connection recovery by restarting RabbitMQ ocassionally lead to a situation when amq.rabbitmq.log in the "/" vhost can be unavailable for a split second. Default vhost may or may not exist in general. So handle {error, not_found} responses from rabbit_basic:publish/4 instead of potentially seriously polluting logs with confusing exceptions. While at it, return a sensible value from terminate/2.
1 parent cd73f98 commit 81ff300

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/rabbit_error_logger.erl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ init([DefaultVHost]) ->
6969
name = ?LOG_EXCH_NAME}}.
7070

7171
terminate(_Arg, _State) ->
72-
terminated_ok.
72+
ok.
7373

7474
code_change(_OldVsn, State, _Extra) ->
7575
{ok, State}.
@@ -105,10 +105,11 @@ publish1(RoutingKey, Format, Data, LogExch) ->
105105

106106
Args = [truncate:term(A, ?LOG_TRUNC) || A <- Data],
107107
Headers = [{<<"node">>, longstr, list_to_binary(atom_to_list(node()))}],
108-
{ok, _DeliveredQPids} =
109-
rabbit_basic:publish(LogExch, RoutingKey,
110-
#'P_basic'{content_type = <<"text/plain">>,
111-
timestamp = Timestamp,
112-
headers = Headers},
113-
list_to_binary(io_lib:format(Format, Args))),
114-
ok.
108+
case rabbit_basic:publish(LogExch, RoutingKey,
109+
#'P_basic'{content_type = <<"text/plain">>,
110+
timestamp = Timestamp,
111+
headers = Headers},
112+
list_to_binary(io_lib:format(Format, Args))) of
113+
{ok, _QPids} -> ok;
114+
{error, _Err} -> ok
115+
end.

0 commit comments

Comments
 (0)