Symfony Autowiring 独白 channel

标签 symfony monolog symfony-3.2

关注此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/

相关文章:

php - Symfony config.yml - 如何按名称传递构造函数参数?

symfony - 如何在服务中使用 kernel.terminate 事件

json - Symfony 3 API REST - try catch JSON 响应格式的异常

php - Symfony 3 表单生成器 : block_name not working

php - 检查 {{ path() }} 是否是 Symfony2 中的当前 {{ path() }}

symfony - 传递给选择字段的实体必须被管理

php - symfony 中的惰性服务不起作用

Symfony2 : best approach to use business (repository) logic in entity or controller

symfony-2.3 - 创建 channel 并将 channel 的日志写入其他文件

php - Laravel 使用 Monolog\Handler\BrowserConsoleHandler 进行日志记录