From f843cf640dbd3da1631aac5966ef32b5f11408c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radovan=20Kep=C3=A1k?= Date: Tue, 27 Sep 2022 13:39:07 +0200 Subject: [PATCH] Added logger --- src/Connection/Client.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Connection/Client.php b/src/Connection/Client.php index 989624a..adc6c66 100644 --- a/src/Connection/Client.php +++ b/src/Connection/Client.php @@ -12,6 +12,7 @@ use Bunny\Protocol; use Contributte\RabbitMQ\Connection\Exception\WaitTimeoutException; use Nette\Utils\Strings; +use Psr\Log\LoggerInterface; use function time; /** @@ -19,12 +20,15 @@ */ class Client extends BunnyClient { + protected ?LoggerInterface $logger; + /** * Constructor. * * @param array $options + * @param LoggerInterface|null $logger */ - public function __construct(array $options = []) + public function __construct(array $options = [], ?LoggerInterface $logger = null) { parent::__construct($options); @@ -34,6 +38,8 @@ public function __construct(array $options = []) $this->options['heartbeat_callback'] = is_callable($options['heartbeat_callback']) ? $options['heartbeat_callback'] : null; + + $this->logger = $logger; } /** @@ -45,6 +51,7 @@ public function sendHeartbeat(): void $this->flushWriteBuffer(); $this->options['heartbeat_callback'] && $this->options['heartbeat_callback'](); + $this->logger?->debug('Bunny: heartbeat called'); } public function syncDisconnect(int $replyCode = 0, string $replyText = ""): bool @@ -78,6 +85,7 @@ protected function write(): void { if ($this->stream && feof($this->stream)) { $this->syncDisconnect(Constants::STATUS_RESOURCE_ERROR, "Connection closed by server unexpectedly"); + $this->logger?->debug('Bunny: Broken pipe detected, server closed stream'); throw new ClientException("Broken pipe or closed connection."); } @@ -89,6 +97,9 @@ protected function write(): void } error_clear_last(); + $this->logger?->debug('Bunny: Broken pipe detected, send of bytes failed', [ + 'last_error' => $last, + ]); throw new ClientException('Broken pipe or closed connection.'); } } @@ -213,4 +224,10 @@ public function waitForConfirm(int $channel, ?int $timeout = null): Protocol\Met $this->enqueue($frame); } } + + protected function feedReadBuffer(): bool + { + $this->logger?->debug('Bunny: read buffer called'); + return parent::feedReadBuffer(); + } }