Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Added logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bckp committed Sep 27, 2022
1 parent 8e8bf67 commit f843cf6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Connection/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
use Bunny\Protocol;
use Contributte\RabbitMQ\Connection\Exception\WaitTimeoutException;
use Nette\Utils\Strings;
use Psr\Log\LoggerInterface;
use function time;

/**
* @codeCoverageIgnore
*/
class Client extends BunnyClient
{
protected ?LoggerInterface $logger;

/**
* Constructor.
*
* @param array<string|mixed> $options
* @param LoggerInterface|null $logger
*/
public function __construct(array $options = [])
public function __construct(array $options = [], ?LoggerInterface $logger = null)
{
parent::__construct($options);

Expand All @@ -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;
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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.');
}
}
Expand Down Expand Up @@ -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();
}
}

0 comments on commit f843cf6

Please sign in to comment.