How to Log Messages to different Files

How to Log Messages to different Files

The Symfony Framework organizes log messages into channels. By default, there are several channels, including doctrine, event, security, request and more. The channel is printed in the log message and can also be used to direct different channels to different places/files.

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

Note

Each channel corresponds to a different logger service (monolog.logger.XXX) Use the php bin/console debug:container monolog command to see a full list of services and learn how to autowire monolog channels.

Switching a Channel to a different Handler

Now, suppose you want to log the security channel to a different file. To do this, create a new handler and configure it to log only messages from the security channel. The following example does that only in the prod configuration environment but you can do it in any (or all) environments:

  • YAML

    1. # config/packages/prod/monolog.yaml
    2. monolog:
    3. handlers:
    4. security:
    5. # log all messages (since debug is the lowest level)
    6. level: debug
    7. type: stream
    8. path: '%kernel.logs_dir%/security.log'
    9. channels: [security]
    10. # an example of *not* logging security channel messages for this handler
    11. main:
    12. # ...
    13. # channels: ['!security']
  • XML

    1. <!-- config/packages/prod/monolog.xml-->
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:monolog="http://symfony.com/schema/dic/monolog"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/monolog
    8. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    9. <monolog:config>
    10. <monolog:handler name="security" type="stream" path="%kernel.logs_dir%/security.log">
    11. <monolog:channels>
    12. <monolog:channel>security</monolog:channel>
    13. </monolog:channels>
    14. </monolog:handler>
    15. <monolog:handler name="main" type="stream" path="%kernel.logs_dir%/main.log">
    16. <!-- ... -->
    17. <monolog:channels>
    18. <monolog:channel>!security</monolog:channel>
    19. </monolog:channels>
    20. </monolog:handler>
    21. </monolog:config>
    22. </container>
  • PHP

    1. // config/packages/prod/monolog.php
    2. $container->loadFromExtension('monolog', [
    3. 'handlers' => [
    4. 'security' => [
    5. 'type' => 'stream',
    6. 'path' => '%kernel.logs_dir%/security.log',
    7. 'channels' => [
    8. 'security',
    9. ],
    10. ],
    11. 'main' => [
    12. // ...
    13. 'channels' => [
    14. '!security',
    15. ],
    16. ],
    17. ],
    18. ]);

Caution

The channels configuration only works for top-level handlers. Handlers that are nested inside a group, buffer, filter, fingers crossed or other such handler will ignore this configuration and will process every message passed to them.

YAML Specification

You can specify the configuration by many forms:

  1. channels: ~ # Include all the channels
  2. channels: foo # Include only channel 'foo'
  3. channels: '!foo' # Include all channels, except 'foo'
  4. channels: [foo, bar] # Include only channels 'foo' and 'bar'
  5. 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

You can also configure additional channels without the need to tag your services:

  • YAML

    1. # config/packages/prod/monolog.yaml
    2. monolog:
    3. channels: ['foo', 'bar']
  • XML

    1. <!-- config/packages/prod/monolog.xml -->
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:monolog="http://symfony.com/schema/dic/monolog"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/monolog
    8. https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
    9. <monolog:config>
    10. <monolog:channel>foo</monolog:channel>
    11. <monolog:channel>bar</monolog:channel>
    12. </monolog:config>
    13. </container>
  • PHP

    1. // config/packages/prod/monolog.php
    2. $container->loadFromExtension('monolog', [
    3. 'channels' => [
    4. 'foo',
    5. 'bar',
    6. ],
    7. ]);

Symfony automatically registers one service per channel (in this example, the channel foo creates a service called monolog.logger.foo). In order to inject this service into others, you must update the service configuration to choose the specific service to inject.

How to Autowire Logger Channels

Starting from MonologBundle 3.5 you can autowire different Monolog channels by type-hinting your service arguments with the following syntax: Psr\Log\LoggerInterface $<channel>Logger. The <channel> must have been predefined in your Monolog configuration.

For example to inject the service related to the app logger channel, change your constructor like this:

  1. - public function __construct(LoggerInterface $logger)
  2. + public function __construct(LoggerInterface $appLogger)
  3. {
  4. $this->logger = $appLogger;
  5. }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.