symfony / monolog-bundle

Symfony Monolog Bundle

Home Page:symfony.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Logger channel changes on its own

LucasWerner437 opened this issue · comments

I've been struggling with logger autowiring with specific channel name in variable name. I have a logger that I want to inject in an EventSubscriber so I tried it with Symfony doc on monolog bundle.

So first I set my channel and handler :

monolog:
  channels: ['google']
  google:
      level: debug
      type: rotating_file
      path: '%kernel.logs_dir%/google/google-%kernel.environment%.log'
      channels: ["google"]
      max_files: 2

Then I try to inject it in my Subscriber's constructor. Knowing that my Subscriber will have its logger, I implement LoggerAwareInterface and use LoggerAwareTrait :

final class GoogleAdsExceptionListener implements EventSubscriberInterface, LoggerAwareInterface
{
    use LoggerAwareTrait;
    
    public function __construct(LoggerInterface $googleLogger)
    {
        $this->setLogger($googleLogger);
    }
}

Since I couldn't see any of the logs I asked it to write in my google log file, I dumped the logger just to make sure it was set correctly. I was a little surprised to see the wrong channel here :

[GoogleAdsExceptionListener.php](http://tendanceelectro.numeriweb.com/netngo/_profiler/open?file=src\EventListener\GoogleAdsExceptionListener.php&line=26#line26) on line 26:
[Symfony\Bridge\Monolog\Logger](http://tendanceelectro.numeriweb.com/netngo/_profiler/open?file=vendor\symfony\monolog-bridge\Logger.php&line=23#line23) {[#17684 ▼]()
  #name: "app"
  #handlers: array:1 [[▶]()]
  #processors: array:1 [[▶]()]
  #microsecondTimestamps: true
  #timezone: DateTimeZone {[#3908 ▶]()}
  #exceptionHandler: null
}

Actually the channel stayed to App, without me knowing anything about why it happened. First I thought that it was a wiring problem then I wired my services by hand, nothing changed.

At some point I tried to remove the LoggerAwareInterface and the use of LoggerAwareTrait, simply creating a private property in my Subscriber and affecting the $googleLoader received in the constructor. And that actually worked :

final class GoogleAdsExceptionListener implements EventSubscriberInterface
{
    private LoggerInterface $logger;

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

And the dump :

[GoogleAdsExceptionListener.php](http://tendanceelectro.numeriweb.com/netngo/_profiler/open?file=src\EventListener\GoogleAdsExceptionListener.php&line=24#line24) on line 24:
[Symfony\Bridge\Monolog\Logger](http://tendanceelectro.numeriweb.com/netngo/_profiler/open?file=vendor\symfony\monolog-bridge\Logger.php&line=23#line23) {[#3987 ▼]()
  #name: "google"
  #handlers: array:1 [[▶]()]
  #processors: array:1 [[▶]()]
  #microsecondTimestamps: true
  #timezone: DateTimeZone {[#17761 ▶]()}
  #exceptionHandler: null
}

I have to admit I'm a bit surprised. Was this intended ? Am I misunderstading the purpose of LoggerAwareInterface and LoggerAwareTrait ? Or is this some shady glitch ?

I hope this is not too long a post ! Thanks for reading me.

IIRC, the LoggerAwareInterface is autoconfigured to inject the logger through the setter, which would then override the logger you inject in the constructor as the setter is called after the instantiation.

If you use constructor injection for the logger, you don't need LoggerAwareInterface.

Ok so that was a misunderstanding. Thank you for the insight ! Thank you also for the post edit btw. I was editing it when I saw how it ended up but you got quicker than me.