vendor/doctrine/mongodb-odm-bundle/APM/PSRCommandLogger.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MongoDBBundle\APM;
  4. use Doctrine\ODM\MongoDB\APM\CommandLoggerInterface;
  5. use MongoDB\Driver\Monitoring\CommandFailedEvent;
  6. use MongoDB\Driver\Monitoring\CommandStartedEvent;
  7. use MongoDB\Driver\Monitoring\CommandSucceededEvent;
  8. use Psr\Log\LoggerInterface;
  9. use function json_encode;
  10. use function MongoDB\Driver\Monitoring\addSubscriber;
  11. use function MongoDB\Driver\Monitoring\removeSubscriber;
  12. final class PSRCommandLogger implements CommandLoggerInterface
  13. {
  14.     /** @var bool */
  15.     private $registered false;
  16.     /** @var LoggerInterface|null */
  17.     private $logger;
  18.     /** @var string */
  19.     private $prefix;
  20.     public function __construct(?LoggerInterface $loggerstring $prefix 'MongoDB command: ')
  21.     {
  22.         $this->logger $logger;
  23.         $this->prefix $prefix;
  24.     }
  25.     public function register(): void
  26.     {
  27.         if ($this->logger === null || $this->registered) {
  28.             return;
  29.         }
  30.         $this->registered true;
  31.         addSubscriber($this);
  32.     }
  33.     public function unregister(): void
  34.     {
  35.         if (! $this->registered) {
  36.             return;
  37.         }
  38.         removeSubscriber($this);
  39.         $this->registered false;
  40.     }
  41.     public function commandStarted(CommandStartedEvent $event)
  42.     {
  43.         if (! $this->logger) {
  44.             return;
  45.         }
  46.         $this->logger->debug($this->prefix json_encode($event->getCommand()));
  47.     }
  48.     public function commandSucceeded(CommandSucceededEvent $event)
  49.     {
  50.     }
  51.     public function commandFailed(CommandFailedEvent $event)
  52.     {
  53.     }
  54. }