MongoDBFormatter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Formatter;
  11. use MongoDB\BSON\UTCDateTime;
  12. use Monolog\Utils;
  13. /**
  14. * Formats a record for use with the MongoDBHandler.
  15. *
  16. * @author Florian Plattner <me@florianplattner.de>
  17. */
  18. class MongoDBFormatter implements FormatterInterface
  19. {
  20. private $exceptionTraceAsString;
  21. private $maxNestingLevel;
  22. private $isLegacyMongoExt;
  23. /**
  24. * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2
  25. * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings
  26. */
  27. public function __construct(int $maxNestingLevel = 3, bool $exceptionTraceAsString = true)
  28. {
  29. $this->maxNestingLevel = max($maxNestingLevel, 0);
  30. $this->exceptionTraceAsString = $exceptionTraceAsString;
  31. $this->isLegacyMongoExt = version_compare(phpversion('mongodb'), '1.1.9', '<=');
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function format(array $record): array
  37. {
  38. return $this->formatArray($record);
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function formatBatch(array $records): array
  44. {
  45. foreach ($records as $key => $record) {
  46. $records[$key] = $this->format($record);
  47. }
  48. return $records;
  49. }
  50. /**
  51. * @return array|string Array except when max nesting level is reached then a string "[...]"
  52. */
  53. protected function formatArray(array $record, int $nestingLevel = 0)
  54. {
  55. if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
  56. foreach ($record as $name => $value) {
  57. if ($value instanceof \DateTimeInterface) {
  58. $record[$name] = $this->formatDate($value, $nestingLevel + 1);
  59. } elseif ($value instanceof \Throwable) {
  60. $record[$name] = $this->formatException($value, $nestingLevel + 1);
  61. } elseif (is_array($value)) {
  62. $record[$name] = $this->formatArray($value, $nestingLevel + 1);
  63. } elseif (is_object($value)) {
  64. $record[$name] = $this->formatObject($value, $nestingLevel + 1);
  65. }
  66. }
  67. } else {
  68. $record = '[...]';
  69. }
  70. return $record;
  71. }
  72. protected function formatObject($value, int $nestingLevel)
  73. {
  74. $objectVars = get_object_vars($value);
  75. $objectVars['class'] = Utils::getClass($value);
  76. return $this->formatArray($objectVars, $nestingLevel);
  77. }
  78. protected function formatException(\Throwable $exception, int $nestingLevel)
  79. {
  80. $formattedException = [
  81. 'class' => Utils::getClass($exception),
  82. 'message' => $exception->getMessage(),
  83. 'code' => (int) $exception->getCode(),
  84. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  85. ];
  86. if ($this->exceptionTraceAsString === true) {
  87. $formattedException['trace'] = $exception->getTraceAsString();
  88. } else {
  89. $formattedException['trace'] = $exception->getTrace();
  90. }
  91. return $this->formatArray($formattedException, $nestingLevel);
  92. }
  93. protected function formatDate(\DateTimeInterface $value, int $nestingLevel): UTCDateTime
  94. {
  95. if ($this->isLegacyMongoExt) {
  96. return $this->legacyGetMongoDbDateTime($value);
  97. }
  98. return $this->getMongoDbDateTime($value);
  99. }
  100. private function getMongoDbDateTime(\DateTimeInterface $value): UTCDateTime
  101. {
  102. return new UTCDateTime((int) (string) floor($value->format('U.u') * 1000));
  103. }
  104. /**
  105. * This is needed to support MongoDB Driver v1.19 and below
  106. *
  107. * See https://github.com/mongodb/mongo-php-driver/issues/426
  108. *
  109. * It can probably be removed in 2.1 or later once MongoDB's 1.2 is released and widely adopted
  110. */
  111. private function legacyGetMongoDbDateTime(\DateTimeInterface $value): UTCDateTime
  112. {
  113. $milliseconds = floor($value->format('U.u') * 1000);
  114. $milliseconds = (PHP_INT_SIZE == 8) //64-bit OS?
  115. ? (int) $milliseconds
  116. : (string) $milliseconds;
  117. return new UTCDateTime($milliseconds);
  118. }
  119. }