ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
20 {
23 
29  {
30  $this->maxNestingLevel = max($maxNestingLevel, 0);
31  $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
32  }
33 
37  public function format(array $record)
38  {
39  return $this->formatArray($record);
40  }
41 
45  public function formatBatch(array $records)
46  {
47  foreach ($records as $key => $record) {
48  $records[$key] = $this->format($record);
49  }
50 
51  return $records;
52  }
53 
54  protected function formatArray(array $record, $nestingLevel = 0)
55  {
56  if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
57  foreach ($record as $name => $value) {
58  if ($value instanceof \DateTime) {
59  $record[$name] = $this->formatDate($value, $nestingLevel + 1);
60  } elseif ($value instanceof \Exception) {
61  $record[$name] = $this->formatException($value, $nestingLevel + 1);
62  } elseif (is_array($value)) {
63  $record[$name] = $this->formatArray($value, $nestingLevel + 1);
64  } elseif (is_object($value)) {
65  $record[$name] = $this->formatObject($value, $nestingLevel + 1);
66  }
67  }
68  } else {
69  $record = '[...]';
70  }
71 
72  return $record;
73  }
74 
75  protected function formatObject($value, $nestingLevel)
76  {
77  $objectVars = get_object_vars($value);
78  $objectVars['class'] = get_class($value);
79 
80  return $this->formatArray($objectVars, $nestingLevel);
81  }
82 
83  protected function formatException(\Exception $exception, $nestingLevel)
84  {
85  $formattedException = array(
86  'class' => get_class($exception),
87  'message' => $exception->getMessage(),
88  'code' => $exception->getCode(),
89  'file' => $exception->getFile() . ':' . $exception->getLine(),
90  );
91 
92  if ($this->exceptionTraceAsString === true) {
93  $formattedException['trace'] = $exception->getTraceAsString();
94  } else {
95  $formattedException['trace'] = $exception->getTrace();
96  }
97 
98  return $this->formatArray($formattedException, $nestingLevel);
99  }
100 
101  protected function formatDate(\DateTime $value, $nestingLevel)
102  {
103  return new \MongoDate($value->getTimestamp());
104  }
105 }
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)
Create styles array
The data for the language used.
formatDate(\DateTime $value, $nestingLevel)
formatArray(array $record, $nestingLevel=0)
formatException(\Exception $exception, $nestingLevel)
formatBatch(array $records)
Formats a set of log records.A set of records to format mixed The formatted set of records ...