Skip to content

How to Define a Custom Logging Formatter

Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (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
# app/config/config_prod.yml (and/or config_dev.yml)
monolog:
    handlers:
        file:
            type: stream
            level: debug
            formatter: 'monolog.formatter.json'
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version