0

Following the symfony documentation: https://symfony.com/doc/current/logging/channels_handlers.html I've created custom handlers to log console command in separate files:

console-live:
    type: console
    process_psr_3_messages: false
    channels: console
console:
    type: stream
    level: debug
    path: "%kernel.logs_dir%/console.log"
    channels: console
    formatter: monolog.formatter.console_command
console-error:
    type: stream
    level: error
    path: "%kernel.logs_dir%/console-error.log"
    channels: console
    formatter: monolog.formatter.console_command

Then I can autowire the console logger in my command like this: LoggerInterface $consoleLogger

But I'm facing an issue, here is an example:

    public function __construct(Helper $helper, LoggerInterface $consoleLogger)
    {
        parent::__construct();
        $this->helper = $helper;
        $this->logger = $consoleLogger;
    }

Any log in my command is correctly logged to the console channel. But since the Helper service autowire the default logger:

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

any log in the helper service will be logged in default logger.

What is the best solution ? Maybe the only way is to initialize a service without autowire so I can pass the caller logger to the service ?

Is there any possibility to use the caller logger automatically ?

Andy Preston
  • 779
  • 4
  • 9
  • 23
Baptiste
  • 1,688
  • 1
  • 15
  • 25

0 Answers0