Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Logging
  4. How to Define a Custom Logging Formatter

How to Define a Custom Logging Formatter

Edit this page

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'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- config/packages/prod/monolog.xml (and/or config/packages/dev/monolog.xml) -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:monolog="http://symfony.com/schema/dic/monolog"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/monolog
        https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

    <monolog:config>
        <monolog:handler
            name="file"
            type="stream"
            level="debug"
            formatter="monolog.formatter.json"
        />
    </monolog:config>
</container>
1
2
3
4
5
6
7
8
9
10
// config/packages/prod/monolog.php (and/or config/packages/dev/monolog.php)
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog): void {
    $monolog->handler('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 format
  • monolog.formatter.gelf_message: serializes a format to GELF format
  • monolog.formatter.html: formats a record into an HTML table
  • monolog.formatter.json: serializes a record into a JSON object
  • monolog.formatter.line: formats a record into a one-line string
  • monolog.formatter.loggly: formats a record information into JSON in a format compatible with Loggly
  • monolog.formatter.logstash: serializes a record to Logstash Event Format
  • monolog.formatter.normalizer: normalizes a record to remove objects/resources so it's easier to dump to various targets
  • monolog.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
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Online Sylius certification, take it now!

    Online Sylius certification, take it now!

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Symfony footer

    Avatar of inspiran, a Symfony contributor

    Thanks inspiran for being a Symfony contributor

    1 commit • 71 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony