@@ -209,10 +209,10 @@ def _set_state(self, new_state: ConnectionState) -> None:
209209 previous_state = self .state
210210 self .state = new_state
211211 _LOGGER .info (
212- "Connection state changed: %r -> %r" ,
212+ "[Connection:%s] Connection state changed: %r -> %r" ,
213+ self ._network_trace_params ["amqpConnection" ],
213214 previous_state ,
214- new_state ,
215- extra = self ._network_trace_params
215+ new_state
216216 )
217217 for session in self ._outgoing_endpoints .values ():
218218 session ._on_connection_state_change () # pylint:disable=protected-access
@@ -321,7 +321,11 @@ def _send_frame(self, channel: int, frame: NamedTuple, **kwargs: Any) -> None:
321321 except Exception : # pylint:disable=try-except-raise
322322 raise
323323 else :
324- _LOGGER .info ("Cannot write frame in current state: %r" , self .state , extra = self ._network_trace_params )
324+ _LOGGER .info (
325+ "[Connection:%s] Cannot write frame in current state: %r" ,
326+ self ._network_trace_params ["amqpConnection" ],
327+ self .state
328+ )
325329
326330 def _get_next_outgoing_channel (self ) -> int :
327331 """Get the next available outgoing channel number within the max channel limit.
@@ -338,7 +342,10 @@ def _get_next_outgoing_channel(self) -> int:
338342 def _outgoing_empty (self ) -> None :
339343 """Send an empty frame to prevent the connection from reaching an idle timeout."""
340344 if self ._network_trace :
341- _LOGGER .debug ("-> EmptyFrame()" , extra = self ._network_trace_params )
345+ _LOGGER .debug (
346+ "[Connection:%s] -> EmptyFrame()" ,
347+ self ._network_trace_params ["amqpConnection" ]
348+ )
342349 if self ._error :
343350 raise self ._error
344351
@@ -359,7 +366,11 @@ def _outgoing_header(self)-> None:
359366 """Send the AMQP protocol header to initiate the connection."""
360367 self ._last_frame_sent_time = time .time ()
361368 if self ._network_trace :
362- _LOGGER .debug ("-> Header(%r)" , HEADER_FRAME , extra = self ._network_trace_params )
369+ _LOGGER .debug (
370+ "[Connection:%s] -> Header(%r)" ,
371+ self ._network_trace_params ["amqpConnection" ],
372+ HEADER_FRAME
373+ )
363374 self ._transport .write (HEADER_FRAME )
364375
365376 def _incoming_header (self , _ , frame : bytes ) -> None :
@@ -369,7 +380,11 @@ def _incoming_header(self, _, frame: bytes) -> None:
369380 :param bytes frame: The incoming frame.
370381 """
371382 if self ._network_trace :
372- _LOGGER .debug ("<- Header(%r)" , frame , extra = self ._network_trace_params )
383+ _LOGGER .debug (
384+ "[Connection:%s] <- Header(%r)" ,
385+ self ._network_trace_params ["amqpConnection" ],
386+ frame ,
387+ )
373388 if self .state == ConnectionState .START :
374389 self ._set_state (ConnectionState .HDR_RCVD )
375390 elif self .state == ConnectionState .HDR_SENT :
@@ -392,7 +407,11 @@ def _outgoing_open(self) -> None:
392407 properties = self ._properties ,
393408 )
394409 if self ._network_trace :
395- _LOGGER .debug ("-> %r" , open_frame , extra = self ._network_trace_params )
410+ _LOGGER .debug (
411+ "[Connection:%s] -> %r" ,
412+ self ._network_trace_params ["amqpConnection" ],
413+ open_frame
414+ )
396415 self ._send_frame (0 , open_frame )
397416
398417 def _incoming_open (self , channel : int , frame ) -> None :
@@ -418,17 +437,27 @@ def _incoming_open(self, channel: int, frame) -> None:
418437 """
419438 # TODO: Add type hints for full frame tuple contents.
420439 if self ._network_trace :
421- _LOGGER .debug ("<- %r" , OpenFrame (* frame ), extra = self ._network_trace_params )
440+ _LOGGER .debug (
441+ "[Connection:%s] <- %r" ,
442+ self ._network_trace_params ["amqpConnection" ],
443+ OpenFrame (* frame )
444+ )
422445 if channel != 0 :
423- _LOGGER .error ("OPEN frame received on a channel that is not 0." , extra = self ._network_trace_params )
446+ _LOGGER .error (
447+ "[Connection:%s] OPEN frame received on a channel that is not 0." ,
448+ self ._network_trace_params ["amqpConnection" ]
449+ )
424450 self .close (
425451 error = AMQPError (
426452 condition = ErrorCondition .NotAllowed , description = "OPEN frame received on a channel that is not 0."
427453 )
428454 )
429455 self ._set_state (ConnectionState .END )
430456 if self .state == ConnectionState .OPENED :
431- _LOGGER .error ("OPEN frame received in the OPENED state." , extra = self ._network_trace_params )
457+ _LOGGER .error (
458+ "[Connection:%s] OPEN frame received in the OPENED state." ,
459+ self ._network_trace_params ["amqpConnection" ]
460+ )
432461 self .close ()
433462 if frame [4 ]:
434463 self ._remote_idle_timeout = cast (float , frame [4 ] / 1000 ) # Convert to seconds
@@ -447,8 +476,8 @@ def _incoming_open(self, channel: int, frame) -> None:
447476 )
448477 )
449478 _LOGGER .error (
450- "Failed parsing OPEN frame: Max frame size is less than supported minimum." ,
451- extra = self ._network_trace_params
479+ "[Connection:%s] Failed parsing OPEN frame: Max frame size is less than supported minimum." ,
480+ self ._network_trace_params [ "amqpConnection" ]
452481 )
453482 return
454483 self ._remote_max_frame_size = frame [2 ]
@@ -466,15 +495,23 @@ def _incoming_open(self, channel: int, frame) -> None:
466495 description = f"connection is an illegal state: { self .state } " ,
467496 )
468497 )
469- _LOGGER .error ("Connection is an illegal state: %r" , self .state , extra = self ._network_trace_params )
498+ _LOGGER .error (
499+ "[Connection:%s] Connection is an illegal state: %r" ,
500+ self ._network_trace_params ["amqpConnection" ],
501+ self .state
502+ )
470503
471504 def _outgoing_close (self , error : Optional [AMQPError ] = None ) -> None :
472505 """Send a Close frame to shutdown connection with optional error information.
473506 :param ~pyamqp.error.AMQPError or None error: Optional error information.
474507 """
475508 close_frame = CloseFrame (error = error )
476509 if self ._network_trace :
477- _LOGGER .debug ("-> %r" , close_frame , extra = self ._network_trace_params )
510+ _LOGGER .debug (
511+ "[Connection:%s] -> %r" ,
512+ self ._network_trace_params ["amqpConnection" ],
513+ close_frame
514+ )
478515 self ._send_frame (0 , close_frame )
479516
480517 def _incoming_close (self , channel : int , frame : Tuple [Any , ...]) -> None :
@@ -488,7 +525,11 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
488525 :param tuple frame: The incoming Close frame.
489526 """
490527 if self ._network_trace :
491- _LOGGER .debug ("<- %r" , CloseFrame (* frame ), extra = self ._network_trace_params )
528+ _LOGGER .debug (
529+ "[Connection:%s] <- %r" ,
530+ self ._network_trace_params ["amqpConnection" ],
531+ CloseFrame (* frame ),
532+ )
492533 disconnect_states = [
493534 ConnectionState .HDR_RCVD ,
494535 ConnectionState .HDR_EXCH ,
@@ -503,8 +544,8 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
503544 close_error = None
504545 if channel > self ._channel_max :
505546 _LOGGER .error (
506- "CLOSE frame received on a channel greated than support max." ,
507- extra = self ._network_trace_params
547+ "[Connection:%s] CLOSE frame received on a channel greated than support max." ,
548+ self ._network_trace_params [ "amqpConnection" ]
508549 )
509550 close_error = AMQPError (condition = ErrorCondition .InvalidField , description = "Invalid channel" , info = None )
510551
@@ -517,8 +558,9 @@ def _incoming_close(self, channel: int, frame: Tuple[Any, ...]) -> None:
517558 condition = frame [0 ][0 ], description = frame [0 ][1 ], info = frame [0 ][2 ]
518559 )
519560 _LOGGER .error (
520- "Connection closed with error: %r" , frame [0 ],
521- extra = self ._network_trace_params
561+ "[Connection:%s] Connection closed with error: %r" ,
562+ self ._network_trace_params ["amqpConnection" ],
563+ frame [0 ],
522564 )
523565
524566
@@ -577,8 +619,8 @@ def _incoming_end(self, channel: int, frame: Tuple[Any, ...]) -> None:
577619 description = "Invalid channel number received"
578620 ))
579621 _LOGGER .error (
580- "END frame received on invalid channel. Closing connection." ,
581- extra = self ._network_trace_params
622+ "[Connection:%s] END frame received on invalid channel. Closing connection." ,
623+ self ._network_trace_params [ "amqpConnection" ]
582624 )
583625 return
584626
@@ -643,7 +685,11 @@ def _process_incoming_frame(self, channel: int, frame: Optional[Union[bytes, Tup
643685 return True
644686 if performative == 1 :
645687 return False
646- _LOGGER .error ("Unrecognized incoming frame: %r" , frame , extra = self ._network_trace_params )
688+ _LOGGER .error (
689+ "[Connection:%s] Unrecognized incoming frame: %r" ,
690+ self ._network_trace_params ["amqpConnection" ],
691+ frame
692+ )
647693 return True
648694 except KeyError :
649695 return True # TODO: channel error
@@ -675,8 +721,8 @@ def _process_outgoing_frame(self, channel: int, frame) -> None:
675721 cast (float , self ._last_frame_received_time ),
676722 ) or self ._get_remote_timeout (now ):
677723 _LOGGER .info (
678- "No frame received for the idle timeout. Closing connection." ,
679- extra = self ._network_trace_params
724+ "[Connection:%s] No frame received for the idle timeout. Closing connection." ,
725+ self ._network_trace_params [ "amqpConnection" ]
680726 )
681727 self .close (
682728 error = AMQPError (
@@ -755,8 +801,8 @@ def listen(self, wait: Union[float, bool] = False, batch: int = 1, **kwargs: Any
755801 now
756802 ):
757803 _LOGGER .info (
758- "No frame received for the idle timeout. Closing connection." ,
759- extra = self ._network_trace_params
804+ "[Connection:%s] No frame received for the idle timeout. Closing connection." ,
805+ self ._network_trace_params [ "amqpConnection" ]
760806 )
761807 self .close (
762808 error = AMQPError (
@@ -777,9 +823,9 @@ def listen(self, wait: Union[float, bool] = False, batch: int = 1, **kwargs: Any
777823 break
778824 else :
779825 _LOGGER .info (
780- "Connection cannot read frames in this state: %r" ,
781- self .state ,
782- extra = self ._network_trace_params
826+ "[Connection:%s] Connection cannot read frames in this state: %r" ,
827+ self ._network_trace_params [ "amqpConnection" ] ,
828+ self .state
783829 )
784830 break
785831 except (OSError , IOError , SSLError , socket .error ) as exc :
@@ -906,7 +952,11 @@ def close(self, error: Optional[AMQPError] = None, wait: bool = False) -> None:
906952 self ._wait_for_response (wait , ConnectionState .END )
907953 except Exception as exc : # pylint:disable=broad-except
908954 # If error happened during closing, ignore the error and set state to END
909- _LOGGER .info ("An error occurred when closing the connection: %r" , exc , extra = self ._network_trace_params )
955+ _LOGGER .info (
956+ "[Connection:%s] An error occurred when closing the connection: %r" ,
957+ self ._network_trace_params ["amqpConnection" ],
958+ exc
959+ )
910960 self ._set_state (ConnectionState .END )
911961 finally :
912962 self ._disconnect ()
0 commit comments