ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
JsonFormatter.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
12namespace Monolog\Formatter;
13
14use Exception;
15
24{
25 const BATCH_MODE_JSON = 1;
27
28 protected $batchMode;
29 protected $appendNewline;
33 protected $includeStacktraces = false;
34
38 public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true)
39 {
40 $this->batchMode = $batchMode;
41 $this->appendNewline = $appendNewline;
42 }
43
53 public function getBatchMode()
54 {
55 return $this->batchMode;
56 }
57
63 public function isAppendingNewlines()
64 {
66 }
67
71 public function format(array $record)
72 {
73 return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
74 }
75
79 public function formatBatch(array $records)
80 {
81 switch ($this->batchMode) {
82 case static::BATCH_MODE_NEWLINES:
83 return $this->formatBatchNewlines($records);
84
85 case static::BATCH_MODE_JSON:
86 default:
87 return $this->formatBatchJson($records);
88 }
89 }
90
94 public function includeStacktraces($include = true)
95 {
96 $this->includeStacktraces = $include;
97 }
98
105 protected function formatBatchJson(array $records)
106 {
107 return $this->toJson($this->normalize($records), true);
108 }
109
117 protected function formatBatchNewlines(array $records)
118 {
119 $instance = $this;
120
121 $oldNewline = $this->appendNewline;
122 $this->appendNewline = false;
123 array_walk($records, function (&$value, $key) use ($instance) {
124 $value = $instance->format($value);
125 });
126 $this->appendNewline = $oldNewline;
127
128 return implode("\n", $records);
129 }
130
138 protected function normalize($data)
139 {
140 if (is_array($data) || $data instanceof \Traversable) {
141 $normalized = array();
142
143 $count = 1;
144 foreach ($data as $key => $value) {
145 if ($count++ >= 1000) {
146 $normalized['...'] = 'Over 1000 items, aborting normalization';
147 break;
148 }
149 $normalized[$key] = $this->normalize($value);
150 }
151
152 return $normalized;
153 }
154
155 if ($data instanceof Exception) {
156 return $this->normalizeException($data);
157 }
158
159 return $data;
160 }
161
170 protected function normalizeException($e)
171 {
172 // TODO 2.0 only check for Throwable
173 if (!$e instanceof Exception && !$e instanceof \Throwable) {
174 throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
175 }
176
177 $data = array(
178 'class' => get_class($e),
179 'message' => $e->getMessage(),
180 'code' => $e->getCode(),
181 'file' => $e->getFile().':'.$e->getLine(),
182 );
183
184 if ($this->includeStacktraces) {
185 $trace = $e->getTrace();
186 foreach ($trace as $frame) {
187 if (isset($frame['file'])) {
188 $data['trace'][] = $frame['file'].':'.$frame['line'];
189 } elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
190 // We should again normalize the frames, because it might contain invalid items
191 $data['trace'][] = $frame['function'];
192 } else {
193 // We should again normalize the frames, because it might contain invalid items
194 $data['trace'][] = $this->normalize($frame);
195 }
196 }
197 }
198
199 if ($previous = $e->getPrevious()) {
200 $data['previous'] = $this->normalizeException($previous);
201 }
202
203 return $data;
204 }
205}
An exception for terminatinating execution or to throw for unit testing.
Encodes whatever record data is passed to it as json.
isAppendingNewlines()
True if newlines are appended to every formatted record.
formatBatch(array $records)
{{Formats a set of log records.mixed The formatted set of records}}
normalizeException($e)
Normalizes given exception with or without its own stack trace based on includeStacktraces property.
format(array $record)
{{Formats a log record.mixed The formatted record}}
normalize($data)
Normalizes given $data.
formatBatchNewlines(array $records)
Use new lines to separate records instead of a JSON-encoded array.
getBatchMode()
The batch mode option configures the formatting style for multiple records.
formatBatchJson(array $records)
Return a JSON-encoded array of records.
__construct($batchMode=self::BATCH_MODE_JSON, $appendNewline=true)
Normalizes incoming records to remove objects/resources so it's easier to dump to various targets.
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
$records
Definition: simple_test.php:22