How to Define a Custom Logging Formatter
Warning: You are browsing the documentation for Symfony 7.0, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
Each logging handler uses a Formatter
to format the record before logging
it. All Monolog handlers use an instance of
Monolog\Formatter\LineFormatter
by default but you can replace it.
Your formatter must implement Monolog\Formatter\FormatterInterface
.
For example, to use the built-in JsonFormatter
, register it as a service then
configure your handler to use it:
1 2 3 4 5 6 7
# config/packages/prod/monolog.yaml (and/or config/packages/dev/monolog.yaml)
monolog:
handlers:
file:
type: stream
level: debug
formatter: 'monolog.formatter.json'
Many built-in formatters are available in Monolog. A lot of them are declared as services
and can be used in the formatter
option:
monolog.formatter.chrome_php
: formats a record according to the ChromePHP array formatmonolog.formatter.gelf_message
: serializes a format to GELF formatmonolog.formatter.html
: formats a record into an HTML tablemonolog.formatter.json
: serializes a record into a JSON objectmonolog.formatter.line
: formats a record into a one-line stringmonolog.formatter.loggly
: formats a record information into JSON in a format compatible with Logglymonolog.formatter.logstash
: serializes a record to Logstash Event Formatmonolog.formatter.normalizer
: normalizes a record to remove objects/resources so it's easier to dump to various targetsmonolog.formatter.scalar
: formats a record into an associative array of scalar (+ null) values (objects and arrays will be JSON encoded)monolog.formatter.wildfire
: serializes a record according to Wildfire's header requirements