MongoDBFormatter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  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. /**
  22. * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2
  23. * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings
  24. */
  25. public function __construct($maxNestingLevel = 3, $exceptionTraceAsString = true)
  26. {
  27. $this->maxNestingLevel = max($maxNestingLevel, 0);
  28. $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function format(array $record)
  34. {
  35. return $this->formatArray($record);
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function formatBatch(array $records)
  41. {
  42. foreach ($records as $key => $record) {
  43. $records[$key] = $this->format($record);
  44. }
  45. return $records;
  46. }
  47. protected function formatArray(array $record, $nestingLevel = 0)
  48. {
  49. if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
  50. foreach ($record as $name => $value) {
  51. if ($value instanceof \DateTimeInterface) {
  52. $record[$name] = $this->formatDate($value, $nestingLevel + 1);
  53. } elseif ($value instanceof \Throwable) {
  54. $record[$name] = $this->formatException($value, $nestingLevel + 1);
  55. } elseif (is_array($value)) {
  56. $record[$name] = $this->formatArray($value, $nestingLevel + 1);
  57. } elseif (is_object($value)) {
  58. $record[$name] = $this->formatObject($value, $nestingLevel + 1);
  59. }
  60. }
  61. } else {
  62. $record = '[...]';
  63. }
  64. return $record;
  65. }
  66. protected function formatObject($value, $nestingLevel)
  67. {
  68. $objectVars = get_object_vars($value);
  69. $objectVars['class'] = get_class($value);
  70. return $this->formatArray($objectVars, $nestingLevel);
  71. }
  72. protected function formatException(\Throwable $exception, $nestingLevel)
  73. {
  74. $formattedException = array(
  75. 'class' => get_class($exception),
  76. 'message' => $exception->getMessage(),
  77. 'code' => $exception->getCode(),
  78. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  79. );
  80. if ($this->exceptionTraceAsString === true) {
  81. $formattedException['trace'] = $exception->getTraceAsString();
  82. } else {
  83. $formattedException['trace'] = $exception->getTrace();
  84. }
  85. return $this->formatArray($formattedException, $nestingLevel);
  86. }
  87. protected function formatDate(\DateTimeInterface $value, $nestingLevel)
  88. {
  89. $seconds = (int) $value->format('U');
  90. $milliseconds = (int) $value->format('u') / 1000;
  91. if ($seconds < 0) {
  92. return new UTCDateTime($seconds * 1000 - $milliseconds);
  93. } else {
  94. return new UTCDateTime($seconds * 1000 + $milliseconds);
  95. }
  96. }
  97. }