How to Define a Custom Logging Formatter

Version: 4.0
Edit this page

Warning: You are browsing the documentation for version 4.0 which is not maintained anymore.

If some of your projects are still using this version, consider upgrading.

How to Define a Custom Logging Formatter

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

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
# config/services.yaml
services:
    # ...

    Monolog\Formatter\JsonFormatter: ~

# config/packages/prod/monolog.yaml (and/or config/packages/dev/monolog.yaml)
monolog:
    handlers:
        file:
            type: stream
            level: debug
            formatter: Monolog\Formatter\JsonFormatter
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.