Documentation

JsonFormatter extends NormalizerFormatter
in package

Encodes whatever record data is passed to it as json

This can be useful to log to databases or remote APIs

Tags
author

Jordi Boggiano j.boggiano@seld.be

phpstan-import-type

Record from \Monolog\Logger

Table of Contents

BATCH_MODE_JSON  = 1
BATCH_MODE_NEWLINES  = 2
SIMPLE_DATE  = "Y-m-d\TH:i:sP"
$appendNewline  : bool
$batchMode  : self::BATCH_MODE_*
$dateFormat  : string
$ignoreEmptyContextAndExtra  : bool
$includeStacktraces  : bool
$maxNormalizeDepth  : int
$maxNormalizeItemCount  : int
$jsonEncodeOptions  : int
__construct()  : mixed
addJsonEncodeOption()  : self
format()  : mixed
Formats a log record.
formatBatch()  : mixed
Formats a set of log records.
getBatchMode()  : int
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.
getDateFormat()  : string
getMaxNormalizeDepth()  : int
The maximum number of normalization levels to go through
getMaxNormalizeItemCount()  : int
The maximum number of items to normalize per level
includeStacktraces()  : self
isAppendingNewlines()  : bool
True if newlines are appended to every formatted record
removeJsonEncodeOption()  : self
setDateFormat()  : self
setJsonPrettyPrint()  : self
Enables `json_encode` pretty print.
setMaxNormalizeDepth()  : self
setMaxNormalizeItemCount()  : self
formatBatchJson()  : string
Return a JSON-encoded array of records.
formatBatchNewlines()  : string
Use new lines to separate records instead of a JSON-encoded array.
formatDate()  : string
normalize()  : mixed
Normalizes given $data.
normalizeException()  : array<string|int, mixed>
Normalizes given exception with or without its own stack trace based on `includeStacktraces` property.
toJson()  : string
Return the JSON representation of a value

Constants

Properties

$ignoreEmptyContextAndExtra

protected bool $ignoreEmptyContextAndExtra

$includeStacktraces

protected bool $includeStacktraces = false

$jsonEncodeOptions

private int $jsonEncodeOptions = MonologUtils::DEFAULT_JSON_FLAGS

Methods

__construct()

public __construct([self::BATCH_MODE_* $batchMode = self::BATCH_MODE_JSON ][, bool $appendNewline = true ][, bool $ignoreEmptyContextAndExtra = false ][, bool $includeStacktraces = false ]) : mixed
Parameters
$batchMode : self::BATCH_MODE_* = self::BATCH_MODE_JSON
$appendNewline : bool = true
$ignoreEmptyContextAndExtra : bool = false
$includeStacktraces : bool = false
Return values
mixed

addJsonEncodeOption()

public addJsonEncodeOption(int $option) : self
Parameters
$option : int
Return values
self

format()

Formats a log record.

public format(array<string|int, mixed> $record) : mixed
Parameters
$record : array<string|int, mixed>
Return values
mixed

The formatted record

formatBatch()

Formats a set of log records.

public formatBatch(array<string|int, mixed> $records) : mixed
Parameters
$records : array<string|int, mixed>

A set of records to format

Return values
mixed

The formatted set of records

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.

public getBatchMode() : int
Return values
int

getMaxNormalizeDepth()

The maximum number of normalization levels to go through

public getMaxNormalizeDepth() : int
Return values
int

getMaxNormalizeItemCount()

The maximum number of items to normalize per level

public getMaxNormalizeItemCount() : int
Return values
int

includeStacktraces()

public includeStacktraces([bool $include = true ]) : self
Parameters
$include : bool = true
Return values
self

isAppendingNewlines()

True if newlines are appended to every formatted record

public isAppendingNewlines() : bool
Return values
bool

removeJsonEncodeOption()

public removeJsonEncodeOption(int $option) : self
Parameters
$option : int
Return values
self

setDateFormat()

public setDateFormat(string $dateFormat) : self
Parameters
$dateFormat : string
Return values
self

setJsonPrettyPrint()

Enables `json_encode` pretty print.

public setJsonPrettyPrint(bool $enable) : self
Parameters
$enable : bool
Return values
self

setMaxNormalizeDepth()

public setMaxNormalizeDepth(int $maxNormalizeDepth) : self
Parameters
$maxNormalizeDepth : int
Return values
self

setMaxNormalizeItemCount()

public setMaxNormalizeItemCount(int $maxNormalizeItemCount) : self
Parameters
$maxNormalizeItemCount : int
Return values
self

formatBatchJson()

Return a JSON-encoded array of records.

protected formatBatchJson(array<string|int, mixed> $records) : string
Parameters
$records : array<string|int, mixed>
Tags
phpstan-param

Record[] $records

Return values
string

formatBatchNewlines()

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

protected formatBatchNewlines(array<string|int, mixed> $records) : string
Parameters
$records : array<string|int, mixed>
Tags
phpstan-param

Record[] $records

Return values
string

formatDate()

protected formatDate(DateTimeInterface $date) : string
Parameters
$date : DateTimeInterface
Return values
string

normalize()

Normalizes given $data.

protected normalize(mixed $data, int $depth) : mixed
Parameters
$data : mixed
$depth : int
Return values
mixed

normalizeException()

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

protected normalizeException(Throwable $e, int $depth) : array<string|int, mixed>
Parameters
$e : Throwable
$depth : int
Return values
array<string|int, mixed>

toJson()

Return the JSON representation of a value

protected toJson(mixed $data[, bool $ignoreErrors = false ]) : string
Parameters
$data : mixed
$ignoreErrors : bool = false
Tags
throws
RuntimeException

if encoding fails and errors are not ignored

Return values
string

if encoding fails and ignoreErrors is true 'null' is returned

Search results