DoctrineMongoDBLogger.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Symfony\Bundle\DoctrineMongoDBBundle\Logger;
  3. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  4. use Symfony\Component\Yaml\Yaml;
  5. /**
  6. * Logger for the Doctrine MongoDB ODM.
  7. *
  8. * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  9. */
  10. class DoctrineMongoDBLogger
  11. {
  12. const LOG_PREFIX = 'MongoDB query: ';
  13. protected $logger;
  14. protected $nbQueries;
  15. public function __construct(LoggerInterface $logger = null)
  16. {
  17. $this->logger = $logger;
  18. $this->nbQueries = 0;
  19. }
  20. public function logQuery($query)
  21. {
  22. ++$this->nbQueries;
  23. if (null !== $this->logger) {
  24. $this->logger->info(static::LOG_PREFIX.static::formatQuery($query));
  25. }
  26. }
  27. public function getNbQueries()
  28. {
  29. return $this->nbQueries;
  30. }
  31. public function getQueries()
  32. {
  33. if (null === $this->logger) {
  34. return false;
  35. }
  36. $logger = $this->logger->getDebugLogger();
  37. if (!$logger) {
  38. return false;
  39. }
  40. $offset = strlen(static::LOG_PREFIX);
  41. $mapper = function($log) use($offset)
  42. {
  43. if (0 === strpos($log['message'], DoctrineMongoDBLogger::LOG_PREFIX)) {
  44. return substr($log['message'], $offset);
  45. }
  46. };
  47. // map queries from logs, remove empty entries and re-index the array
  48. return array_values(array_filter(array_map($mapper, $logger->getLogs())));
  49. }
  50. /**
  51. * Formats the supplied query array recursively.
  52. *
  53. * @param array $query All or part of a query array
  54. *
  55. * @return string A serialized object for the log
  56. */
  57. static protected function formatQuery(array $query, $array = true)
  58. {
  59. $parts = array();
  60. foreach ($query as $key => $value) {
  61. if (!is_numeric($key)) {
  62. $array = false;
  63. }
  64. if (is_bool($value)) {
  65. $formatted = $value ? 'true' : 'false';
  66. } elseif (is_scalar($value)) {
  67. $formatted = '"'.$value.'"';
  68. } elseif (is_array($value)) {
  69. $formatted = static::formatQuery($value);
  70. } elseif ($value instanceof \MongoId) {
  71. $formatted = 'ObjectId("'.$value.'")';
  72. } elseif ($value instanceof \MongoDate) {
  73. $formatted = 'new Date("'.date('r', $value->sec).'")';
  74. } elseif ($value instanceof \DateTime) {
  75. $formatted = 'new Date("'.date('r', $value->getTimestamp()).'")';
  76. } elseif ($value instanceof \MongoRegex) {
  77. $formatted = 'new RegExp("'.$value->regex.'", "'.$value->flags.'")';
  78. } elseif ($value instanceof \MongoMinKey) {
  79. $formatted = 'new MinKey()';
  80. } elseif ($value instanceof \MongoMaxKey) {
  81. $formatted = 'new MaxKey()';
  82. } elseif ($value instanceof \MongoBinData) {
  83. $formatted = 'new BinData("'.$value->bin.'", "'.$value->type.'")';
  84. } elseif ($value instanceof \stdClass) {
  85. $formatted = static::formatQuery((array) $value, false);
  86. } else {
  87. $formatted = (string) $value;
  88. }
  89. $parts['"'.$key.'"'] = $formatted;
  90. }
  91. if (0 == count($parts)) {
  92. return $array ? '[ ]' : '{ }';
  93. }
  94. if ($array) {
  95. return '[ '.implode(', ', $parts).' ]';
  96. } else {
  97. $mapper = function($key, $value)
  98. {
  99. return $key.': '.$value;
  100. };
  101. return '{ '.implode(', ', array_map($mapper, array_keys($parts), array_values($parts))).' }';
  102. }
  103. }
  104. }