ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Monolog\Formatter\JsonFormatter Class Reference

Encodes whatever record data is passed to it as json. More...

+ Inheritance diagram for Monolog\Formatter\JsonFormatter:
+ Collaboration diagram for Monolog\Formatter\JsonFormatter:

Public Member Functions

 __construct ($batchMode=self::BATCH_MODE_JSON, $appendNewline=true)
 
 getBatchMode ()
 The batch mode option configures the formatting style for multiple records. More...
 
 isAppendingNewlines ()
 True if newlines are appended to every formatted record. More...
 
 format (array $record)
 {Formats a log record.
Parameters
array$recordA record to format
Returns
mixed The formatted record
} More...
 
 formatBatch (array $records)
 {Formats a set of log records.
Parameters
array$recordsA set of records to format
Returns
mixed The formatted set of records
} More...
 
 includeStacktraces ($include=true)
 
- Public Member Functions inherited from Monolog\Formatter\NormalizerFormatter
 __construct ($dateFormat=null)
 
 format (array $record)
 {Formats a log record.
Parameters
array$recordA record to format
Returns
mixed The formatted record
} More...
 
 formatBatch (array $records)
 {Formats a set of log records.
Parameters
array$recordsA set of records to format
Returns
mixed The formatted set of records
} More...
 
 detectAndCleanUtf8 (&$data)
 Detect invalid UTF-8 string characters and convert to valid UTF-8. More...
 

Data Fields

const BATCH_MODE_JSON = 1
 
const BATCH_MODE_NEWLINES = 2
 
- Data Fields inherited from Monolog\Formatter\NormalizerFormatter
const SIMPLE_DATE = "Y-m-d H:i:s"
 

Protected Member Functions

 formatBatchJson (array $records)
 Return a JSON-encoded array of records. More...
 
 formatBatchNewlines (array $records)
 Use new lines to separate records instead of a JSON-encoded array. More...
 
 normalize ($data, $depth=0)
 Normalizes given $data. More...
 
 normalizeException ($e)
 Normalizes given exception with or without its own stack trace based on includeStacktraces property. More...
 
- Protected Member Functions inherited from Monolog\Formatter\NormalizerFormatter
 normalize ($data, $depth=0)
 
 normalizeException ($e)
 
 toJson ($data, $ignoreErrors=false)
 Return the JSON representation of a value. More...
 

Protected Attributes

 $batchMode
 
 $appendNewline
 
 $includeStacktraces = false
 
- Protected Attributes inherited from Monolog\Formatter\NormalizerFormatter
 $dateFormat
 

Detailed Description

Encodes whatever record data is passed to it as json.

This can be useful to log to databases or remote APIs

Author
Jordi Boggiano j.bog.nosp@m.gian.nosp@m.o@sel.nosp@m.d.be

Definition at line 25 of file JsonFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Formatter\JsonFormatter::__construct (   $batchMode = self::BATCH_MODE_JSON,
  $appendNewline = true 
)
Parameters
int$batchMode
bool$appendNewline

Definition at line 42 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$appendNewline, and Monolog\Formatter\JsonFormatter\$batchMode.

43  {
44  $this->batchMode = $batchMode;
45  $this->appendNewline = $appendNewline;
46  }

Member Function Documentation

◆ format()

Monolog\Formatter\JsonFormatter::format ( array  $record)

{Formats a log record.

Parameters
array$recordA record to format
Returns
mixed The formatted record
}

Implements Monolog\Formatter\FormatterInterface.

Definition at line 75 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\normalize(), and Monolog\Formatter\NormalizerFormatter\toJson().

Referenced by Monolog\Formatter\JsonFormatterTest\formatRecordWithExceptionInContext().

76  {
77  return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
78  }
normalize($data, $depth=0)
Normalizes given $data.
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ formatBatch()

Monolog\Formatter\JsonFormatter::formatBatch ( array  $records)

{Formats a set of log records.

Parameters
array$recordsA set of records to format
Returns
mixed The formatted set of records
}

Implements Monolog\Formatter\FormatterInterface.

Definition at line 83 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\formatBatchJson(), and Monolog\Formatter\JsonFormatter\formatBatchNewlines().

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  }
formatBatchNewlines(array $records)
Use new lines to separate records instead of a JSON-encoded array.
formatBatchJson(array $records)
Return a JSON-encoded array of records.
$records
Definition: simple_test.php:22
+ Here is the call graph for this function:

◆ formatBatchJson()

Monolog\Formatter\JsonFormatter::formatBatchJson ( array  $records)
protected

Return a JSON-encoded array of records.

Parameters
array$records
Returns
string

Definition at line 109 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\normalize(), and Monolog\Formatter\NormalizerFormatter\toJson().

Referenced by Monolog\Formatter\JsonFormatter\formatBatch().

110  {
111  return $this->toJson($this->normalize($records), true);
112  }
normalize($data, $depth=0)
Normalizes given $data.
$records
Definition: simple_test.php:22
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ formatBatchNewlines()

Monolog\Formatter\JsonFormatter::formatBatchNewlines ( array  $records)
protected

Use new lines to separate records instead of a JSON-encoded array.

Parameters
array$records
Returns
string

Definition at line 121 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$appendNewline, and $key.

Referenced by Monolog\Formatter\JsonFormatter\formatBatch().

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  }
$records
Definition: simple_test.php:22
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ getBatchMode()

Monolog\Formatter\JsonFormatter::getBatchMode ( )

The batch mode option configures the formatting style for multiple records.

By default, multiple records will be formatted as a JSON-encoded array. However, for compatibility with some API endpoints, alternative styles are available.

Returns
int

Definition at line 57 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$batchMode.

◆ includeStacktraces()

Monolog\Formatter\JsonFormatter::includeStacktraces (   $include = true)
Parameters
bool$include

Definition at line 98 of file JsonFormatter.php.

Referenced by Monolog\Formatter\JsonFormatter\normalizeException().

99  {
100  $this->includeStacktraces = $include;
101  }
+ Here is the caller graph for this function:

◆ isAppendingNewlines()

Monolog\Formatter\JsonFormatter::isAppendingNewlines ( )

True if newlines are appended to every formatted record.

Returns
bool

Definition at line 67 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$appendNewline.

◆ normalize()

Monolog\Formatter\JsonFormatter::normalize (   $data,
  $depth = 0 
)
protected

Normalizes given $data.

Parameters
mixed$data
Returns
mixed

Definition at line 142 of file JsonFormatter.php.

References $data, $key, and Monolog\Formatter\JsonFormatter\normalizeException().

Referenced by Monolog\Formatter\JsonFormatter\format(), Monolog\Formatter\JsonFormatter\formatBatchJson(), and Monolog\Formatter\JsonFormatter\normalizeException().

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  }
normalize($data, $depth=0)
Normalizes given $data.
normalizeException($e)
Normalizes given exception with or without its own stack trace based on includeStacktraces property...
$key
Definition: croninfo.php:18
$data
Definition: bench.php:6
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ normalizeException()

Monolog\Formatter\JsonFormatter::normalizeException (   $e)
protected

Normalizes given exception with or without its own stack trace based on includeStacktraces property.

Parameters
Exception | Throwable$e
Returns
array

Definition at line 179 of file JsonFormatter.php.

References $data, Monolog\Utils\getClass(), Monolog\Formatter\JsonFormatter\includeStacktraces(), and Monolog\Formatter\JsonFormatter\normalize().

Referenced by Monolog\Formatter\JsonFormatter\normalize().

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  }
normalize($data, $depth=0)
Normalizes given $data.
static getClass($object)
Definition: Utils.php:19
normalizeException($e)
Normalizes given exception with or without its own stack trace based on includeStacktraces property...
$data
Definition: bench.php:6
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $appendNewline

◆ $batchMode

Monolog\Formatter\JsonFormatter::$batchMode
protected

◆ $includeStacktraces

Monolog\Formatter\JsonFormatter::$includeStacktraces = false
protected

Definition at line 36 of file JsonFormatter.php.

◆ BATCH_MODE_JSON

◆ BATCH_MODE_NEWLINES


The documentation for this class was generated from the following file: