ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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...
 
 format (array $record)
 Formats a log record. More...
 
 formatBatch (array $records)
 Formats a set of log records. 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)
 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)
 
 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 23 of file JsonFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

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

Reimplemented in Monolog\Formatter\LogglyFormatter.

Definition at line 38 of file JsonFormatter.php.

39 {
40 $this->batchMode = $batchMode;
41 $this->appendNewline = $appendNewline;
42 }

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

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
}}

Reimplemented from Monolog\Formatter\NormalizerFormatter.

Reimplemented in Monolog\Formatter\LogglyFormatter.

Definition at line 71 of file JsonFormatter.php.

72 {
73 return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
74 }
normalize($data)
Normalizes given $data.
toJson($data, $ignoreErrors=false)
Return the JSON representation of a value.

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

+ Here is the call 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
}}

Reimplemented from Monolog\Formatter\NormalizerFormatter.

Definition at line 79 of file JsonFormatter.php.

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 }
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

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

+ 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 105 of file JsonFormatter.php.

106 {
107 return $this->toJson($this->normalize($records), true);
108 }

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

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

+ 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 117 of file JsonFormatter.php.

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 }

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

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

+ 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 53 of file JsonFormatter.php.

54 {
55 return $this->batchMode;
56 }

References Monolog\Formatter\JsonFormatter\$batchMode.

◆ includeStacktraces()

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

Definition at line 94 of file JsonFormatter.php.

95 {
96 $this->includeStacktraces = $include;
97 }

References Monolog\Formatter\JsonFormatter\includeStacktraces().

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

+ Here is the call graph for this function:
+ 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 63 of file JsonFormatter.php.

64 {
66 }

References Monolog\Formatter\JsonFormatter\$appendNewline.

◆ normalize()

Monolog\Formatter\JsonFormatter::normalize (   $data)
protected

Normalizes given $data.

Parameters
mixed$data
Returns
mixed

Reimplemented from Monolog\Formatter\NormalizerFormatter.

Definition at line 138 of file JsonFormatter.php.

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 }
normalizeException($e)
Normalizes given exception with or without its own stack trace based on includeStacktraces property.

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

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

+ 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

Reimplemented from Monolog\Formatter\NormalizerFormatter.

Definition at line 170 of file JsonFormatter.php.

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 }

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

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

+ 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 33 of file JsonFormatter.php.

◆ BATCH_MODE_JSON

◆ BATCH_MODE_NEWLINES


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