Refactor logging

This commit is contained in:
2021-09-18 22:47:06 +03:00
parent c8cd41777f
commit 97b3d8206c
4 changed files with 23 additions and 31 deletions

View File

@ -43,17 +43,9 @@ abstract class Connection
private LoggerInterface $logger, private LoggerInterface $logger,
) { ) {
$this->stream = new NullStream(); $this->stream = new NullStream();
$this->onConnectCallback = static function () use ($logger, $address) { $this->onConnectCallback = static function () {
$logger->debug('Connected.', [
'class' => static::class,
'address' => $address,
]);
}; };
$this->onCloseCallback = static function () use ($logger, $address) { $this->onCloseCallback = static function () {
$logger->debug('Disconnected.', [
'class' => static::class,
'address' => $address,
]);
}; };
} }

View File

@ -38,6 +38,15 @@ final class Consumer extends Connection
); );
$this->onMessage = $onMessage; $this->onMessage = $onMessage;
$context = compact('address', 'topic', 'channel');
$this->onConnect(function () use ($context) {
$this->logger->debug('Consumer connected.', $context);
});
$this->onClose(function () use ($context) {
$this->logger->debug('Consumer disconnected.', $context);
});
$this->logger->debug('Consumer created.', $context);
} }
public static function create( public static function create(
@ -68,12 +77,6 @@ final class Consumer extends Connection
return call(function (): \Generator { return call(function (): \Generator {
yield parent::connect(); yield parent::connect();
$this->logger->debug('Consumer {topic}:{channel} connected to {host}', [
'topic' => $this->topic,
'channel' => $this->channel,
'host' => $this->address,
]);
$this->run(); $this->run();
}); });
} }
@ -129,12 +132,6 @@ final class Consumer extends Connection
} }
} }
$this->logger->debug('Consumer disconnected.', [
'address' => $this->address,
'topic' => $this->topic,
'channel' => $this->channel,
]);
$this->close(false); $this->close(false);
}); });
}); });

View File

@ -144,8 +144,6 @@ final class Lookup
unset($consumers[$consumerKey]); unset($consumers[$consumerKey]);
})->connect(); })->connect();
$this->logger->debug('Consumer created.', compact('address', 'topic', 'channel'));
$promise->onResolve(function (?\Throwable $e) use ($consumerKey, &$consumers) { $promise->onResolve(function (?\Throwable $e) use ($consumerKey, &$consumers) {
if (null !== $e) { if (null !== $e) {
$this->logger->error($e->getMessage()); $this->logger->error($e->getMessage());
@ -163,13 +161,13 @@ final class Lookup
$this->watch($topic); $this->watch($topic);
$this->logger->info('Subscribed', compact('topic', 'channel')); $this->logger->info('Subscribed.', compact('topic', 'channel'));
} }
public function unsubscribe(string $topic, string $channel): void public function unsubscribe(string $topic, string $channel): void
{ {
if (null === ($this->running[$topic][$channel] ?? null)) { if (null === ($this->running[$topic][$channel] ?? null)) {
$this->logger->debug('Trying unsubscribe from non subscribed', compact('topic', 'channel')); $this->logger->debug('Not subscribed.', compact('topic', 'channel'));
return; return;
} }
@ -180,7 +178,7 @@ final class Lookup
unset($this->running[$topic]); unset($this->running[$topic]);
} }
$this->logger->info('Unsubscribed', compact('topic', 'channel')); $this->logger->info('Unsubscribed.', compact('topic', 'channel'));
} }
private function watch(string $topic): void private function watch(string $topic): void

View File

@ -24,6 +24,15 @@ final class Producer extends Connection
$this->clientConfig, $this->clientConfig,
$this->logger, $this->logger,
); );
$context = compact('address');
$this->onConnect(function () use ($context) {
$this->logger->debug('Producer connected.', $context);
});
$this->onClose(function () use ($context) {
$this->logger->debug('Producer disconnected.', $context);
});
$this->logger->debug('Producer created.', $context);
} }
public static function create( public static function create(
@ -48,10 +57,6 @@ final class Producer extends Connection
return call(function (): \Generator { return call(function (): \Generator {
yield parent::connect(); yield parent::connect();
$this->logger->debug('Producer connected to {host}', [
'host' => $this->address,
]);
$this->run(); $this->run();
}); });
} }