DbalLogger.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Symfony\Component\HttpKernel\Log\LoggerInterface;
  12. use Symfony\Component\HttpKernel\Debug\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. protected $logger;
  22. protected $stopwatch;
  23. /**
  24. * Constructor.
  25. *
  26. * @param LoggerInterface $logger A LoggerInterface instance
  27. * @param Stopwatch $stopwatch A Stopwatch instance
  28. */
  29. public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null)
  30. {
  31. $this->logger = $logger;
  32. $this->stopwatch = $stopwatch;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function startQuery($sql, array $params = null, array $types = null)
  38. {
  39. if (null !== $this->stopwatch) {
  40. $this->stopwatch->start('doctrine', 'doctrine');
  41. }
  42. if (null !== $this->logger) {
  43. $this->log($sql, null === $params ? array() : $params);
  44. }
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function stopQuery()
  50. {
  51. if (null !== $this->stopwatch) {
  52. $this->stopwatch->stop('doctrine');
  53. }
  54. }
  55. /**
  56. * Logs a message.
  57. *
  58. * @param string $message A message to log
  59. * @param array $params The context
  60. */
  61. protected function log($message, array $params)
  62. {
  63. $this->logger->debug($message, $params);
  64. }
  65. }