Skip to content

Commit

Permalink
Refactor logging implementation (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Feb 3, 2024
1 parent 29b9ba0 commit f9bf752
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
use FriendsOfHyperf\Trigger\Subscriber\TriggerSubscriber;
use FriendsOfHyperf\Trigger\Traits\Logger;
use Hyperf\Collection\Arr;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Constants;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coroutine\Coroutine;
use MySQLReplication\Config\ConfigBuilder;
use MySQLReplication\MySQLReplicationFactory;
use MySQLReplication\Socket\SocketException;
use Psr\Log\LoggerInterface;

use function Hyperf\Support\make;
use function Hyperf\Support\retry;
Expand All @@ -36,6 +36,8 @@ class Consumer

protected ?string $name = null;

protected ?LoggerInterface $logger = null;

private ?HealthMonitor $healthMonitor = null;

private ?ServerMutexInterface $serverMutex = null;
Expand All @@ -48,8 +50,7 @@ public function __construct(
protected SubscriberManager $subscriberManager,
protected TriggerManager $triggerManager,
protected string $connection = 'default',
protected array $options = [],
protected ?StdoutLoggerInterface $logger = null
protected array $options = []
) {
if (isset($options['name'])) {
$this->name = $options['name'];
Expand All @@ -70,6 +71,8 @@ public function __construct(
if ($this->getOption('health_monitor.enable', true)) {
$this->healthMonitor = make(HealthMonitor::class, ['consumer' => $this]);
}

$this->logger = $this->getLogger();
}

public function start(): void
Expand Down
16 changes: 16 additions & 0 deletions src/Contact/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Trigger\Contact;

interface LoggerInterface extends \Psr\Log\LoggerInterface
{
}
12 changes: 4 additions & 8 deletions src/Monitor/HealthMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use FriendsOfHyperf\Trigger\Event\OnReplicationStop;
use FriendsOfHyperf\Trigger\Snapshot\BinLogCurrentSnapshotInterface;
use FriendsOfHyperf\Trigger\Traits\Logger;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\CoordinatorManager;
use Hyperf\Coordinator\Timer;
use Hyperf\Coroutine\Coroutine;
use MySQLReplication\BinLog\BinLogCurrent;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;

class HealthMonitor
{
Expand All @@ -39,20 +39,16 @@ class HealthMonitor

protected Timer $timer;

protected ?StdoutLoggerInterface $logger = null;
protected ?LoggerInterface $logger = null;

public function __construct(protected ContainerInterface $container, protected Consumer $consumer)
{
$this->connection = $consumer->getConnection();
$this->monitorInterval = (int) $consumer->getOption('health_monitor.interval', 10);
$this->snapShortInterval = (int) $consumer->getOption('snapshot.interval', 10);
$this->binLogCurrentSnapshot = $consumer->getBinLogCurrentSnapshot();
if ($container->has(StdoutLoggerInterface::class)) {
$this->logger = $container->get(StdoutLoggerInterface::class);
}
$this->timer = new Timer(
$this->logger instanceof StdoutLoggerInterface ? $this->logger : null
);
$this->logger = $this->getLogger();
$this->timer = new Timer($this->logger);
}

public function process(): void
Expand Down
13 changes: 10 additions & 3 deletions src/SubscriberManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
namespace FriendsOfHyperf\Trigger;

use FriendsOfHyperf\Trigger\Annotation\Subscriber;
use FriendsOfHyperf\Trigger\Traits\Logger;
use Hyperf\Collection\Arr;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Stdlib\SplPriorityQueue;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class SubscriberManager
{
use Logger;

protected array $subscribers = [];

public function __construct(protected ?StdoutLoggerInterface $logger = null)
protected ?LoggerInterface $logger = null;

public function __construct(protected ContainerInterface $container)
{
$this->logger = $this->getLogger();
}

public function register()
Expand All @@ -40,7 +47,7 @@ public function register()
$this->subscribers[$property->connection] ??= [];
$this->subscribers[$property->connection][] = $class;

$this->logger->debug(sprintf(
$this->logger?->debug(sprintf(
'[trigger.%s] %s registered by %s process by %s.',
$property->connection,
$this::class,
Expand Down
11 changes: 6 additions & 5 deletions src/Traits/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Trigger\Traits;

use FriendsOfHyperf\Trigger\Contact\LoggerInterface as ContactLoggerInterface;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -61,10 +62,10 @@ protected function getLogger(): ?LoggerInterface

$container = ApplicationContext::getContainer();

if ($container->has(StdoutLoggerInterface::class)) {
return $container->get(StdoutLoggerInterface::class);
}

return null;
return match (true) {
$container->has(ContactLoggerInterface::class) => $container->get(ContactLoggerInterface::class),
$container->has(StdoutLoggerInterface::class) => $container->get(StdoutLoggerInterface::class),
default => null,
};
}
}

0 comments on commit f9bf752

Please sign in to comment.