scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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 /**
15 * Formats a record for use with the MongoDBHandler.
16 *
17 * @author Florian Plattner <me@florianplattner.de>
18 */
19 class MongoDBFormatter implements FormatterInterface
20 {
21 private $exceptionTraceAsString;
22 private $maxNestingLevel;
23  
24 /**
25 * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2
26 * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings
27 */
28 public function __construct($maxNestingLevel = 3, $exceptionTraceAsString = true)
29 {
30 $this->maxNestingLevel = max($maxNestingLevel, 0);
31 $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
32 }
33  
34 /**
35 * {@inheritDoc}
36 */
37 public function format(array $record)
38 {
39 return $this->formatArray($record);
40 }
41  
42 /**
43 * {@inheritDoc}
44 */
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 }