ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
12 namespace Monolog\Formatter;
13 
14 use Exception;
15 use Monolog\Utils;
16 use Throwable;
17 
26 {
27  const BATCH_MODE_JSON = 1;
29 
30  protected $batchMode;
31  protected $appendNewline;
32 
36  protected $includeStacktraces = false;
37 
42  public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true)
43  {
44  $this->batchMode = $batchMode;
45  $this->appendNewline = $appendNewline;
46  }
47 
57  public function getBatchMode()
58  {
59  return $this->batchMode;
60  }
61 
67  public function isAppendingNewlines()
68  {
69  return $this->appendNewline;
70  }
71 
75  public function format(array $record)
76  {
77  return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
78  }
79 
83  public function formatBatch(array $records)
84  {
85  switch ($this->batchMode) {
86  case static::BATCH_MODE_NEWLINES:
87  return $this->formatBatchNewlines($records);
88 
89  case static::BATCH_MODE_JSON:
90  default:
91  return $this->formatBatchJson($records);
92  }
93  }
94 
98  public function includeStacktraces($include = true)
99  {
100  $this->includeStacktraces = $include;
101  }
102 
109  protected function formatBatchJson(array $records)
110  {
111  return $this->toJson($this->normalize($records), true);
112  }
113 
121  protected function formatBatchNewlines(array $records)
122  {
123  $instance = $this;
124 
125  $oldNewline = $this->appendNewline;
126  $this->appendNewline = false;
127  array_walk($records, function (&$value, $key) use ($instance) {
128  $value = $instance->format($value);
129  });
130  $this->appendNewline = $oldNewline;
131 
132  return implode("\n", $records);
133  }
134 
142  protected function normalize($data, $depth = 0)
143  {
144  if ($depth > 9) {
145  return 'Over 9 levels deep, aborting normalization';
146  }
147 
148  if (is_array($data) || $data instanceof \Traversable) {
149  $normalized = array();
150 
151  $count = 1;
152  foreach ($data as $key => $value) {
153  if ($count++ > 1000) {
154  $normalized['...'] = 'Over 1000 items ('.count($data).' total), aborting normalization';
155  break;
156  }
157 
158  $normalized[$key] = $this->normalize($value, $depth+1);
159  }
160 
161  return $normalized;
162  }
163 
164  if ($data instanceof Exception || $data instanceof Throwable) {
165  return $this->normalizeException($data);
166  }
167 
168  return $data;
169  }
170 
179  protected function normalizeException($e)
180  {
181  // TODO 2.0 only check for Throwable
182  if (!$e instanceof Exception && !$e instanceof Throwable) {
183  throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.Utils::getClass($e));
184  }
185 
186  $data = array(
187  'class' => Utils::getClass($e),
188  'message' => $e->getMessage(),
189  'code' => $e->getCode(),
190  'file' => $e->getFile().':'.$e->getLine(),
191  );
192 
193  if ($this->includeStacktraces) {
194  $trace = $e->getTrace();
195  foreach ($trace as $frame) {
196  if (isset($frame['file'])) {
197  $data['trace'][] = $frame['file'].':'.$frame['line'];
198  } elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
199  // We should again normalize the frames, because it might contain invalid items
200  $data['trace'][] = $frame['function'];
201  } else {
202  // We should again normalize the frames, because it might contain invalid items
203  $data['trace'][] = $this->normalize($frame);
204  }
205  }
206  }
207 
208  if ($previous = $e->getPrevious()) {
209  $data['previous'] = $this->normalizeException($previous);
210  }
211 
212  return $data;
213  }
214 }
format(array $record)
{Formats a log record.A record to format mixed The formatted record}
getBatchMode()
The batch mode option configures the formatting style for multiple records.
formatBatchNewlines(array $records)
Use new lines to separate records instead of a JSON-encoded array.
normalize($data, $depth=0)
Normalizes given $data.
static getClass($object)
Definition: Utils.php:19
__construct($batchMode=self::BATCH_MODE_JSON, $appendNewline=true)
formatBatchJson(array $records)
Return a JSON-encoded array of records.
$records
Definition: simple_test.php:22
isAppendingNewlines()
True if newlines are appended to every formatted record.
formatBatch(array $records)
{Formats a set of log records.A set of records to format mixed The formatted set of records} ...
normalizeException($e)
Normalizes given exception with or without its own stack trace based on includeStacktraces property...
Normalizes incoming records to remove objects/resources so it&#39;s easier to dump to various targets...
Encodes whatever record data is passed to it as json.
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
$key
Definition: croninfo.php:18
$data
Definition: bench.php:6