Symfony-登录到服务中的多个日志文件

克比

我正在尝试将多个独白处理程序注入服务。现在,我的父班级注入一个记录器,而子班级注入另一个记录器。我的目标是能够将特定操作记录到特定的日志文件中。

我的service.yaml:

App\Services\PrinterManager:
    arguments: ['@doctrine.orm.entity_manager','@logger', '', '', '', '','']
    tags:
        - { name: monolog.logger, channel: printerface}

App\Services\Printer\Printer:
    autowire: true
    autoconfigure: false
    public: false
    parent: App\Services\PrinterManager
    arguments:
        index_2: '@logger'
        index_3: '@oneup_flysystem.printer_invoice_filesystem'
        index_4: '@oneup_flysystem.printerface_content_filesystem'
        index_5: '@oneup_flysystem.sftp_filesystem'
        index_6: '@App\Services\PrinterApiService'
    tags:
        - { name: monolog.logger, channel: printerlog}

我的monolog.yaml:

monolog:
  handlers:
    main:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
        channels: ["!event, !printerface", "!printerlog"]
    printerface:
        type: stream
        level: debug
        channels: ["printerface"]
        path: "%kernel.logs_dir%/printerface.log"
    printerlog:
        type: stream
        level: debug
        channels: ["printerlog"]
        path: "%kernel.logs_dir%/printerlog.log"

但是似乎当前的服务配置破坏了构造函数,并且出现以下错误:

The argument must be an existing index or the name of a constructor's parameter.          

有什么方法可以在服务中使用两个日志文件?

艾丽斯特·布尔曼(Alister Bulman)

我没有使用父/子类完成此操作,但是通过使用命名参数更简单一些,这就是我所拥有的(使用三个不同的记录器):

# App/Subscribers/WebhookLoggingListener.php file
public function __construct(
    LoggerInterface $logger, 
    LoggerInterface $mailgunLog, 
    LoggerInterface $dripLog) {
}

# services.yml
App\Subscribers\WebhookLoggingListener:
    arguments:
        $logger: "@logger"
        $mailgunLog: "@monolog.logger.mailgun"
        $dripLog: "@monolog.logger.drip"
    tags:
       - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

如果我在其他地方使用其他记录器,也可以将它们绑定到特定的变量名称:

services:
    _defaults:
        # ... other config
        bind:
            $dripLog: "@?monolog.logger.drip"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章