MongoDBFormatter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  12. * Formats a record for use with the MongoDBHandler.
  13. *
  14. * @author Florian Plattner <me@florianplattner.de>
  15. */
  16. class MongoDBFormatter implements FormatterInterface
  17. {
  18. private $exceptionTraceAsString;
  19. private $maxNestingLevel;
  20. /**
  21. * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2
  22. * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings
  23. */
  24. public function __construct($maxNestingLevel = 3, $exceptionTraceAsString = true)
  25. {
  26. $this->maxNestingLevel = max($maxNestingLevel, 0);
  27. $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function format(array $record)
  33. {
  34. return $this->formatArray($record);
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function formatBatch(array $records)
  40. {
  41. foreach ($records as $key => $record) {
  42. $records[$key] = $this->format($record);
  43. }
  44. return $records;
  45. }
  46. protected function formatArray(array $record, $nestingLevel = 0)
  47. {
  48. if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
  49. foreach ($record as $name => $value) {
  50. if ($value instanceof \DateTime) {
  51. $record[$name] = $this->formatDate($value, $nestingLevel + 1);
  52. } elseif ($value instanceof \Exception) {
  53. $record[$name] = $this->formatException($value, $nestingLevel + 1);
  54. } elseif (is_array($value)) {
  55. $record[$name] = $this->formatArray($value, $nestingLevel + 1);
  56. } elseif (is_object($value)) {
  57. $record[$name] = $this->formatObject($value, $nestingLevel + 1);
  58. }
  59. }
  60. } else {
  61. $record = '[...]';
  62. }
  63. return $record;
  64. }
  65. protected function formatObject($value, $nestingLevel)
  66. {
  67. $objectVars = get_object_vars($value);
  68. $objectVars['class'] = get_class($value);
  69. return $this->formatArray($objectVars, $nestingLevel);
  70. }
  71. protected function formatException(\Exception $exception, $nestingLevel)
  72. {
  73. $formattedException = array(
  74. 'class' => get_class($exception),
  75. 'message' => $exception->getMessage(),
  76. 'code' => $exception->getCode(),
  77. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  78. );
  79. if ($this->exceptionTraceAsString === true) {
  80. $formattedException['trace'] = $exception->getTraceAsString();
  81. } else {
  82. $formattedException['trace'] = $exception->getTrace();
  83. }
  84. return $this->formatArray($formattedException, $nestingLevel);
  85. }
  86. protected function formatDate(\DateTime $value, $nestingLevel)
  87. {
  88. return new \MongoDate($value->getTimestamp());
  89. }
  90. }