我不确定为什么使用 HandlerWrapper 是错误的做法。
我遇到了同样的问题,并想出了一种方法来包装处理程序以过滤某些记录。
在这个答案中,我描述了解决这个问题的两种方法,一种更复杂,一种更简单。
(或多或少)复杂的方式
我做的第一件事是创建一个新类,它扩展了 HandlerWrapper 并添加了一些可以过滤记录的逻辑:
use Monolog\Handler\HandlerWrapper;
class CustomHandler extends HandlerWrapper
{
public function isHandling(array $record)
{
if ($this->shouldFilter($record)) {
return false;
}
return $this->handler->isHandling($record);
}
public function handle(array $record)
{
if (!$this->isHandling($record)) {
return false;
}
return $this->handler->handle($record);
}
public function handleBatch(array $records)
{
foreach ($records as $record) {
$this->handle($record);
}
}
private function shouldFilter(array $record)
{
return mt_rand(0, 1) === 1;; // add logic here
}
}
然后我创建了一个服务定义和一个 CompilerPass,我可以在其中包装 GroupHandler
services.yml
CustomHandler:
class: CustomHandler
abstract: true
arguments: ['']
use Monolog\Handler\GroupHandler;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
class CustomMonologHandlerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(CustomHandler::class)) {
return;
}
$definitions = $container->getDefinitions();
foreach ($definitions as $serviceId => $definition) {
if (!$this->isValidDefinition($definition)) {
continue;
}
$cacheId = $serviceId . '.wrapper';
$container
->setDefinition($cacheId, new ChildDefinition(CustomHandler::class))
->replaceArgument(0, new Reference($cacheId . '.inner'))
->setDecoratedService($serviceId);
}
}
private function isValidDefinition(Definition $definition): bool
{
return GroupHandler::class === $definition->getClass();
}
}
如您所见,我在这里查看了所有定义,并找到了将 GroupHandler 设置为其类的定义。如果是这种情况,我会在容器中添加一个新定义,用我的 CustomHandler 装饰原始处理程序。
旁注:起初我尝试包装所有处理程序(当然除了 CustomHandler :)),但由于一些处理程序实现了其他接口(例如使用 EventSubscriberInterface 的ConsoleHandler ) 这不起作用,并导致了我不想以某种骇人听闻的方式解决的问题。
不要忘记将此编译器传递添加到 AppBundle 类中的容器
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new CustomMonologHandlerPass());
}
}
现在一切就绪,您必须对处理程序进行分组才能完成这项工作:
app/config(_prod|_dev).yml
monolog:
handlers:
my_group:
type: group
members: [ 'graylog' ]
graylog:
type: gelf
publisher:
id: my.publisher
level: debug
formatter: my.formatter
简单的方法
我们使用与复杂方式相同的 CustomHandler,然后我们在配置中定义我们的处理程序:
app/config(_prod|_dev).yml
monolog:
handlers:
graylog:
type: gelf
publisher:
id: my.publisher
level: debug
formatter: my.formatter
用你自己的 CustomHandler 装饰你的 services.yml 中的处理程序
services.yml
CustomHandler:
class: CustomHandler
decorates: monolog.handler.graylog
arguments: ['@CustomHandler.inner']
对于 decorates 属性,您必须使用格式 monolog.handler.$NAME_SPECIFIED_AS_KEY_IN_CONFIG,在本例中为 graylog。
...就是这样
总结
虽然这两种方法都有效,但我使用了第一种,因为我们有几个 symfony 项目我需要它并装饰所有处理程序
手动不是我想要的。
我希望这会有所帮助(即使我的答案已经很晚了:))