ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MongoDBFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Formatter;
13 
14 use Monolog\Utils;
15 
22 {
25 
31  {
32  $this->maxNestingLevel = max($maxNestingLevel, 0);
33  $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
34  }
35 
39  public function format(array $record)
40  {
41  return $this->formatArray($record);
42  }
43 
47  public function formatBatch(array $records)
48  {
49  foreach ($records as $key => $record) {
50  $records[$key] = $this->format($record);
51  }
52 
53  return $records;
54  }
55 
56  protected function formatArray(array $record, $nestingLevel = 0)
57  {
58  if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
59  foreach ($record as $name => $value) {
60  if ($value instanceof \DateTime) {
61  $record[$name] = $this->formatDate($value, $nestingLevel + 1);
62  } elseif ($value instanceof \Exception) {
63  $record[$name] = $this->formatException($value, $nestingLevel + 1);
64  } elseif (is_array($value)) {
65  $record[$name] = $this->formatArray($value, $nestingLevel + 1);
66  } elseif (is_object($value)) {
67  $record[$name] = $this->formatObject($value, $nestingLevel + 1);
68  }
69  }
70  } else {
71  $record = '[...]';
72  }
73 
74  return $record;
75  }
76 
77  protected function formatObject($value, $nestingLevel)
78  {
79  $objectVars = get_object_vars($value);
80  $objectVars['class'] = Utils::getClass($value);
81 
82  return $this->formatArray($objectVars, $nestingLevel);
83  }
84 
85  protected function formatException(\Exception $exception, $nestingLevel)
86  {
87  $formattedException = array(
88  'class' => Utils::getClass($exception),
89  'message' => $exception->getMessage(),
90  'code' => $exception->getCode(),
91  'file' => $exception->getFile() . ':' . $exception->getLine(),
92  );
93 
94  if ($this->exceptionTraceAsString === true) {
95  $formattedException['trace'] = $exception->getTraceAsString();
96  } else {
97  $formattedException['trace'] = $exception->getTrace();
98  }
99 
100  return $this->formatArray($formattedException, $nestingLevel);
101  }
102 
103  protected function formatDate(\DateTime $value, $nestingLevel)
104  {
105  return new \MongoDate($value->getTimestamp());
106  }
107 }
static getClass($object)
Definition: Utils.php:19
Formats a record for use with the MongoDBHandler.
$records
Definition: simple_test.php:22
format(array $record)
Formats a log record.A record to format mixed The formatted record
__construct($maxNestingLevel=3, $exceptionTraceAsString=true)
formatObject($value, $nestingLevel)
formatDate(\DateTime $value, $nestingLevel)
formatArray(array $record, $nestingLevel=0)
formatException(\Exception $exception, $nestingLevel)
$key
Definition: croninfo.php:18
formatBatch(array $records)
Formats a set of log records.A set of records to format mixed The formatted set of records ...