在symfony 2.5.9中,我试图重写Monolog的SwiftMailerHandler
class MySwiftMailerHandler extends SwiftMailerHandler
{
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
{
$message->setSubject('Lorem ipsum : ' . $message->getSubject()) ;
parent::__construct($mailer, $message, $level, $bubble);
}
}有服务
<service id="my_custom_handler" class="XXXX\Monolog\Handler\MySwiftMailerHandler">
<tag name="monolog.handler.swift"/> <!-- which tag to use ? -->
</service>和配置
monolog:
handlers:
mail:
type: fingers_crossed
action_level: critical
handler: custom #before buffered
custom:
type: service
id: my_custom_handler
#buffered:
# type: buffer
# handler: swift
swift:
type: swift_mailer
from_email: %monolog_from_email%
to_email: %monolog_to_email%
subject: 'Error'
level: critical但是我的处理程序有以下错误:"__construct()必须是Swift_Mailer的一个实例,没有给出.“
如何创建新的处理程序服务?糟糕的配置单?要用哪个标签?怎么做?谢谢!
发布于 2015-06-01 20:49:50
这很棘手。所有不同的处理程序类都定义为monolog.xml中的MonologBundle中的参数。(MonologExtension似乎是在一个大开关语句中实例化和配置这些。)
若要重写SwiftMailerHandler,请指定如下类参数:
parameters: ... monolog.handler.swift_mailer.class: XXXX\Monolog\Handler\MySwiftMailerHandler
不需要到处乱搞服务、标签等。
我使用这种方法覆盖SwiftMailerHandler::buildMessage()并动态地更改电子邮件消息的主题。
发布于 2018-05-14 15:19:49
可以使用编译器传递更改类:
Symfony文档:如果您想修改另一个包的服务定义,可以使用编译器传递来更改服务的类或修改方法调用。
// src/Kernel.php
namespace App;
// ...
+ use App\Service\YourService;
+ use Symfony\Component\DependencyInjection\ContainerBuilder;
+ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class Kernel extends BaseKernel implements CompilerPassInterface
{
+ public function process(ContainerBuilder $container)
+ {
+ $definition = $container->findDefinition('monolog.handler.swift');
+ $definition->setClass(YourService::class);
+ }
}https://stackoverflow.com/questions/28634494
复制相似问题