symfony2.3 spool monolog错误电子邮件命令

时间:2014-12-10 17:10:42

标签: email symfony command monolog

我知道这个问题已经在这篇帖子中提出了:Send email when error occurs in console command of Symfony2 app,但答案并没有为手头的问题提供完整的解决方案,我无法评论原帖。

我需要在命令中发送一个monolog错误电子邮件。电子邮件在文件假脱机程序中正确排队;不幸的是,我被迫使用内存卷轴。 奇怪的是,为手动刷新假脱机而提供的代码片段适用于我的代码中生成的电子邮件,而不适用于monolog。 有人知道为什么会发生这种情况吗?是否可以使用内存卷轴?

config.yml:

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    spool:     { type: memory }

# Monolog Configuration
monolog:
    channels: ["account.create"]
    handlers:
        account.create.group:
            type: group
            members: [account.create.streamed, account.create.buffered]
            channels: [account.create]
        account.create.streamed:    
            type: stream
            path: %kernel.logs_dir%/accounts_creation.log
            level: info
        account.create.buffered:
            type: buffer
            handler: account.create.swift
        account.create.swift:
            type: swift_mailer
            from_email: xxx@yyy.com
            to_email: aaa@gmail.com
            subject: 'An Error Occurred while managing zzz!'
            level: critical

config_prod.yml:

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
        nested:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            channels: [!account.create]

用法示例:

try
{
    //code that could block
}
catch(ManageUserBlockingExceptionInterface $e)
{
    $exitCode       = self::EXIT_CODE_ERROR_BLOCKING;

    //le eccezioni bloccanti vengono loggate e non si conferma che
    //il messaggio è stato utilizzato ma si termina la coda
    if(!\is_null($this->logger))
    {
       $this->logger->crit($e->getMessage());
    }
}

通过依赖注入作为服务注入logger:

...
<argument type="service" id="monolog.logger.account.create" on-invalid="null" />
...

并且它有效,因为关键错误在文件日志中流式传输;但如果swiftmailer配置了文件假脱机,也会创建电子邮件。

最后,手动刷新内存假脱机的代码如下:

protected function flushMailSpool()
{
    $mailer                 = $this->container->get('mailer');
    $spool                  = $mailer->getTransport()->getSpool();
    $transport              = $this->container->get('swiftmailer.transport.real');

    $spool->flushQueue($transport);
}

在服务目的发送电子邮件后立即调用;我注意到相同的代码,输入命令并适应命令环境,即$ this-&gt;容器变为$ this-&gt; getContainer()不起作用,可能是由于范围更改?

0 个答案:

没有答案