@@ -208,16 +208,29 @@ def channel(self):
208208 except Exception :
209209 self .logger .exception ("Encountered exception while closing Channel." )
210210
211+ def _ignore_pika_logs (self ) -> None :
212+ """Ensures that pika logs are filtered.
213+
214+ The main thread may keep connections open for a long time
215+ w/o publishing heartbeats, which means that they'll end up
216+ being closed by the time the broker is closed. When that
217+ happens, pika logs a bunch of scary stuff so we want to
218+ filter that out.
219+ """
220+
221+ logging_filter = _IgnoreScaryLogs ()
222+ ignored_loggers = ["pika.adapters.base_connection" , "pika.adapters.blocking_connection" ]
223+
224+ # Make sure the filter is added only once.
225+ for logger_name in ignored_loggers :
226+ ignored_logger = logging .getLogger (logger_name )
227+ if not any (isinstance (f , _IgnoreScaryLogs ) for f in ignored_logger .filters ):
228+ ignored_logger .addFilter (logging_filter )
229+
211230 def close (self ) -> None :
212231 """Close all open RabbitMQ connections."""
213- # The main thread may keep connections open for a long time
214- # w/o publishing heartbeats, which means that they'll end up
215- # being closed by the time the broker is closed. When that
216- # happens, pika logs a bunch of scary stuff so we want to
217- # filter that out.
218- logging_filter = _IgnoreScaryLogs ()
219- logging .getLogger ("pika.adapters.base_connection" ).addFilter (logging_filter )
220- logging .getLogger ("pika.adapters.blocking_connection" ).addFilter (logging_filter )
232+
233+ self ._ignore_pika_logs ()
221234
222235 self .logger .debug ("Closing channels and connections..." )
223236 for channel_or_conn in chain (self .channels , self .connections ):
0 commit comments