MongoDBFormatter.php 4.8 KB

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