src/Hitso/Bundle/CommonBundle/Monitor/SlowQueryLogger.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Maciej Kaczmarek <maciej.kaczmarek@autentika.pl>
  5.  */
  6. namespace Hitso\Bundle\CommonBundle\Monitor;
  7. use Doctrine\DBAL\Logging\DebugStack;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class SlowQueryLogger implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     private $logPath;
  16.     /**
  17.      * @var bool
  18.      */
  19.     private $debug;
  20.     /**
  21.      * @var bool
  22.      */
  23.     private $registered false;
  24.     /**
  25.      * @var DebugStack
  26.      */
  27.     private $logger;
  28.     /**
  29.      * SlowQueryLogger constructor.
  30.      *
  31.      * @param string     $logPath
  32.      * @param string     $lockPath
  33.      * @param bool       $debug
  34.      * @param DebugStack $logger
  35.      */
  36.     public function __construct($logPath$lockPath$debugDebugStack $logger null)
  37.     {
  38.         $this->logger  $logger;
  39.         $this->logPath $logPath;
  40.         $this->debug   $debug;
  41.     }
  42.     public function onRequest()
  43.     {
  44.         if (!$this->registered && !$this->debug && $this->logger && $this->logger->enabled) {
  45.             $this->registered true;
  46.             register_shutdown_function(function () {
  47.                 foreach ($this->logger->queries as $query) {
  48.                     if ($query['executionMS'] > 0.2) {
  49.                         $entry = [
  50.                             'stamp'  => time(),
  51.                             'time'   => round($query['executionMS'] * 1000),
  52.                             'query'  => $query['sql'],
  53.                             'params' => $query['params'],
  54.                             'errors' => false,
  55.                         ];
  56.                         file_put_contents($this->logPathbase64_encode(serialize($entry)) . PHP_EOLFILE_APPEND);
  57.                     }
  58.                 }
  59.             });
  60.         }
  61.     }
  62.     public function getSlowQueries()
  63.     {
  64.         if (!file_exists($this->logPath)) {
  65.             return [];
  66.         }
  67.         $logs array_filter(file($this->logPath));
  68.         unlink($this->logPath);
  69.         $logs array_map(
  70.             function ($entry) {
  71.                 return unserialize(base64_decode($entry));
  72.             },
  73.             $logs
  74.         );
  75.         return array_filter($logs);
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             KernelEvents::REQUEST => ['onRequest'512],
  84.         ];
  85.     }
  86. }