Skip to content

Commit

Permalink
Revert 'Refactor logging implementation (#542)'
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia committed Feb 3, 2024
1 parent 13786ae commit 180e8ed
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
9 changes: 3 additions & 6 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,8 +36,6 @@ class Consumer

protected ?string $name = null;

protected ?LoggerInterface $logger = null;

private ?HealthMonitor $healthMonitor = null;

private ?ServerMutexInterface $serverMutex = null;
Expand All @@ -50,7 +48,8 @@ public function __construct(
protected SubscriberManager $subscriberManager,
protected TriggerManager $triggerManager,
protected string $connection = 'default',
protected array $options = []
protected array $options = [],
protected ?StdoutLoggerInterface $logger = null
) {
if (isset($options['name'])) {
$this->name = $options['name'];
Expand All @@ -71,8 +70,6 @@ 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: 0 additions & 16 deletions src/Contact/LoggerInterface.php

This file was deleted.

12 changes: 8 additions & 4 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,16 +39,20 @@ class HealthMonitor

protected Timer $timer;

protected ?LoggerInterface $logger = null;
protected ?StdoutLoggerInterface $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();
$this->logger = $this->getLogger();
$this->timer = new Timer($this->logger);
if ($container->has(StdoutLoggerInterface::class)) {
$this->logger = $container->get(StdoutLoggerInterface::class);
}
$this->timer = new Timer(
$this->logger instanceof StdoutLoggerInterface ? $this->logger : null
);
}

public function process(): void
Expand Down
13 changes: 3 additions & 10 deletions src/SubscriberManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@
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 Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use SplPriorityQueue;

class SubscriberManager
{
use Logger;

protected array $subscribers = [];

protected ?LoggerInterface $logger = null;

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

public function register()
Expand All @@ -47,7 +40,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: 5 additions & 6 deletions src/Traits/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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 @@ -62,10 +61,10 @@ protected function getLogger(): ?LoggerInterface

$container = ApplicationContext::getContainer();

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

return null;
}
}

0 comments on commit 180e8ed

Please sign in to comment.