2
0

MongoDBFormatter.php 4.4 KB

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