关注此documentation ,我可以创建许多 channel ,这些 channel 将创建具有以下名称 monolog.logger.<channel_name>
的服务
如何通过 DI 注入(inject)和 Autowiring 将这些服务注入(inject)到我的服务中?
class FooService
{
public function __construct(LoggerInterface $loggerInterface) { }
}
Yaml
#existing
foo_service:
class: AppBundle\Services\FooService
arguments: ["@monolog.logger.barchannel"]
# what I want to do
foo_service:
autowire: true # how to inject @monolog.logger.barchannel ?
最佳答案
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
. For example, to inject the service related to the app logger channel use this:public function __construct(LoggerInterface $appLogger) { $this->logger = $appLogger; }
https://symfony.com/doc/current/logging/channels_handlers.html#monolog-autowire-channels
关于Symfony Autowiring 独白 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43788114/