ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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...
 

Data Fields

const BATCH_MODE_JSON = 1
 
const BATCH_MODE_NEWLINES = 2
 

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

Protected Attributes

 $batchMode
 
 $appendNewline
 

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

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 32 of file JsonFormatter.php.

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

33  {
34  $this->batchMode = $batchMode;
35  $this->appendNewline = $appendNewline;
36  }

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

66  {
67  return json_encode($record) . ($this->appendNewline ? "\n" : '');
68  }

◆ 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 73 of file JsonFormatter.php.

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

74  {
75  switch ($this->batchMode) {
76  case static::BATCH_MODE_NEWLINES:
77  return $this->formatBatchNewlines($records);
78 
79  case static::BATCH_MODE_JSON:
80  default:
81  return $this->formatBatchJson($records);
82  }
83  }
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:17
+ 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 91 of file JsonFormatter.php.

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

92  {
93  return json_encode($records);
94  }
$records
Definition: simple_test.php:17
+ 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 103 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$appendNewline.

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

104  {
105  $instance = $this;
106 
107  $oldNewline = $this->appendNewline;
108  $this->appendNewline = false;
109  array_walk($records, function (&$value, $key) use ($instance) {
110  $value = $instance->format($value);
111  });
112  $this->appendNewline = $oldNewline;
113 
114  return implode("\n", $records);
115  }
$records
Definition: simple_test.php:17
+ 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 47 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$batchMode.

◆ isAppendingNewlines()

Monolog\Formatter\JsonFormatter::isAppendingNewlines ( )

True if newlines are appended to every formatted record.

Returns
bool

Definition at line 57 of file JsonFormatter.php.

References Monolog\Formatter\JsonFormatter\$appendNewline.

Field Documentation

◆ $appendNewline

◆ $batchMode

Monolog\Formatter\JsonFormatter::$batchMode
protected

◆ BATCH_MODE_JSON

◆ BATCH_MODE_NEWLINES


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