ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
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  {
65  return $this->appendNewline;
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 }
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.
__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.
normalize($data)
Normalizes given $data.
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...
Create styles array
The data for the language used.
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