vendor/symfony/doctrine-bridge/Logger/DbalLogger.php line 29

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\Logger;
  11. use Doctrine\DBAL\Logging\SQLLogger;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Stopwatch\Stopwatch;
  14. /**
  15.  * @author Fabien Potencier <fabien@symfony.com>
  16.  */
  17. class DbalLogger implements SQLLogger
  18. {
  19.     public const MAX_STRING_LENGTH = 32;
  20.     public const BINARY_DATA_VALUE = '(binary value)';
  21.     protected $logger;
  22.     protected $stopwatch;
  23.     public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null)
  24.     {
  25.         $this->logger = $logger;
  26.         $this->stopwatch = $stopwatch;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function startQuery($sql, array $params = null, array $types = null): void
  32.     {
  33.         if (null !== $this->stopwatch) {
  34.             $this->stopwatch->start('doctrine', 'doctrine');
  35.         }
  36.         if (null !== $this->logger) {
  37.             $this->log($sql, null === $params ? [] : $this->normalizeParams($params));
  38.         }
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function stopQuery(): void
  44.     {
  45.         if (null !== $this->stopwatch) {
  46.             $this->stopwatch->stop('doctrine');
  47.         }
  48.     }
  49.     /**
  50.      * Logs a message.
  51.      */
  52.     protected function log(string $message, array $params)
  53.     {
  54.         $this->logger->debug($message, $params);
  55.     }
  56.     private function normalizeParams(array $params): array
  57.     {
  58.         foreach ($params as $index => $param) {
  59.             // normalize recursively
  60.             if (\is_array($param)) {
  61.                 $params[$index] = $this->normalizeParams($param);
  62.                 continue;
  63.             }
  64.             if (!\is_string($params[$index])) {
  65.                 continue;
  66.             }
  67.             // non utf-8 strings break json encoding
  68.             if (!preg_match('//u', $params[$index])) {
  69.                 $params[$index] = self::BINARY_DATA_VALUE;
  70.                 continue;
  71.             }
  72.             // detect if the too long string must be shorten
  73.             if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], 'UTF-8')) {
  74.                 $params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]';
  75.                 continue;
  76.             }
  77.         }
  78.         return $params;
  79.     }
  80. }