Search code examples
phpsymfonyloggingmonolog

Monolog doesn't write to the right log file


I'm on Symfony 4 and I want to create a dedicated channel for many log types: this is my configuration for channels and handlers:

 monolog:
  channels: ["channel1", "channel2"]

  handlers:
    channel1:
      level: debug
      type:  stream
      path:  "%kernel.logs_dir%/channel1.log"
      channels: ["channel"]

    channel2:
      level: debug
      type:  stream
      path:  "%kernel.logs_dir%/channel2.log"
      channels: ["channel2"]

Then, in my service to write log, I inject the custom

 services:
  _defaults:
    autowire: true
    autoconfigure: true
  Infrastructure\Logger\Channel1Logger:
    arguments:
       - '@monolog.logger.channel1'

  Infrastructure\Logger\Channel2Logger:
    arguments:
      - '@monolog.logger.channel2'

But, all my logs are directly written to the channel "app", When I debug the container, I see my services listed What I'm doing wrong?


Solution

  • I found the error. By default, the config/services.yaml will override all configuration from external files. That's why my log continues to be on the default channel (autowiring). To avoid this, you have to exclude the loggers custom files from autowiring