DbalLogger.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Psr\Log\LoggerInterface;
  12. use Symfony\Component\Stopwatch\Stopwatch;
  13. use Doctrine\DBAL\Logging\SQLLogger;
  14. /**
  15. * DbalLogger.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class DbalLogger implements SQLLogger
  20. {
  21. const MAX_STRING_LENGTH = 32;
  22. const BINARY_DATA_VALUE = '(binary value)';
  23. protected $logger;
  24. protected $stopwatch;
  25. /**
  26. * Constructor.
  27. *
  28. * @param LoggerInterface $logger A LoggerInterface instance
  29. * @param Stopwatch $stopwatch A Stopwatch instance
  30. */
  31. public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null)
  32. {
  33. $this->logger = $logger;
  34. $this->stopwatch = $stopwatch;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function startQuery($sql, array $params = null, array $types = null)
  40. {
  41. if (null !== $this->stopwatch) {
  42. $this->stopwatch->start('doctrine', 'doctrine');
  43. }
  44. if (is_array($params)) {
  45. foreach ($params as $index => $param) {
  46. if (!is_string($params[$index])) {
  47. continue;
  48. }
  49. // non utf-8 strings break json encoding
  50. if (!preg_match('#[\p{L}\p{N} ]#u', $params[$index])) {
  51. $params[$index] = self::BINARY_DATA_VALUE;
  52. continue;
  53. }
  54. // detect if the too long string must be shorten
  55. if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($params[$index])) {
  56. if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], $encoding)) {
  57. $params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, $encoding).' [...]';
  58. continue;
  59. }
  60. } else {
  61. if (self::MAX_STRING_LENGTH < strlen($params[$index])) {
  62. $params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]';
  63. continue;
  64. }
  65. }
  66. }
  67. }
  68. if (null !== $this->logger) {
  69. $this->log($sql, null === $params ? array() : $params);
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function stopQuery()
  76. {
  77. if (null !== $this->stopwatch) {
  78. $this->stopwatch->stop('doctrine');
  79. }
  80. }
  81. /**
  82. * Logs a message.
  83. *
  84. * @param string $message A message to log
  85. * @param array $params The context
  86. */
  87. protected function log($message, array $params)
  88. {
  89. $this->logger->debug($message, $params);
  90. }
  91. }