Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Logging
  5. How to Log Messages to different Files
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Switching a Channel to a different Handler
  • YAML Specification
  • Creating your own Channel
    • Configure Additional Channels without Tagged Services
  • Learn more from the Cookbook

How to Log Messages to different Files

Edit this page

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

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to Log Messages to different Files

The Symfony Standard Edition contains a bunch of channels for logging: doctrine, event, security and request. Each channel corresponds to a logger service (monolog.logger.XXX) in the container and is injected to the concerned service. The purpose of channels is to be able to organize different types of log messages.

By default, Symfony logs every message into a single file (regardless of the channel).

Switching a Channel to a different Handler

Now, suppose you want to log the doctrine channel to a different file.

To do so, just create a new handler and configure it like this:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
# app/config/config.yml
monolog:
    handlers:
        main:
            type:     stream
            path:     /var/log/symfony.log
            channels: ["!doctrine"]
        doctrine:
            type:     stream
            path:     /var/log/doctrine.log
            channels: [doctrine]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- app/config/config.xml -->
<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
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/monolog
        http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"
>
    <monolog:config>
        <monolog:handler name="main" type="stream" path="/var/log/symfony.log">
            <monolog:channels>
                <monolog:channel>!doctrine</monolog:channel>
            </monolog:channels>
        </monolog:handler>

        <monolog:handler name="doctrine" type="stream" path="/var/log/doctrine.log">
            <monolog:channels>
                <monolog:channel>doctrine</monolog:channel>
            </monolog:channels>
        </monolog:handler>
    </monolog:config>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// app/config/config.php
$container->loadFromExtension('monolog', array(
    'handlers' => array(
        'main'     => array(
            'type'     => 'stream',
            'path'     => '/var/log/symfony.log',
            'channels' => array(
                '!doctrine',
            ),
        ),
        'doctrine' => array(
            'type'     => 'stream',
            'path'     => '/var/log/doctrine.log',
            'channels' => array(
                'doctrine',
            ),
        ),
    ),
));

YAML Specification

You can specify the configuration by many forms:

1
2
3
4
5
6
7
channels: ~    # Include all the channels

channels: foo  # Include only channel "foo"
channels: "!foo" # Include all channels, except "foo"

channels: [foo, bar]   # Include only channels "foo" and "bar"
channels: ["!foo", "!bar"] # Include all channels, except "foo" and "bar"

Creating your own Channel

You can change the channel monolog logs to one service at a time. This is done either via the configuration below or by tagging your service with monolog.logger and specifying which channel the service should log to. With the tag, the logger that is injected into that service is preconfigured to use the channel you've specified.

Configure Additional Channels without Tagged Services

2.4

This feature was introduced to the MonologBundle 2.4, which was first packaged with Symfony 2.4.

With MonologBundle 2.4 you can configure additional channels without the need to tag your services:

  • YAML
  • XML
  • PHP
1
2
3
# app/config/config.yml
monolog:
    channels: ["foo", "bar"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- app/config/config.xml -->
<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
        http://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/monolog
        http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"
>
    <monolog:config>
        <monolog:channel>foo</monolog:channel>
        <monolog:channel>bar</monolog:channel>
    </monolog:config>
</container>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('monolog', array(
    'channels' => array(
        'foo',
        'bar',
    ),
));

With this, you can now send log messages to the foo channel by using the automatically registered logger service monolog.logger.foo.

Learn more from the Cookbook

  • How to Use Monolog to Write Logs
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of alsar, a Symfony contributor

Thanks alsar for being a Symfony contributor

1 commit • 8 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?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • 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
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia