您还可以使用错误级别活动策略(实际上 Symfony 的内置 404 错误排除是使用此完成的,所以我想这是一个正确的方法)。
配置.yml
monolog:
handlers:
main:
type: fingers_crossed
handler: loggly
activation_strategy: 'mybundle.monolog.fingers_crossed.activation_strategy'
loggly:
type: loggly
token: %loggly_token%
level: error
tag: %loggly_tag%
services.yml(请注意,此处设置了操作级别,而不是在 config.yml 中)
services:
mybundle.monolog.fingers_crossed.activation_strategy:
class: MyBundle\Handler\FingersCrossed\ErrorLevelActivationStrategy
arguments:
- '@request_stack'
- 'error'
ErrorLevelActivationStrategy.php
<?php
namespace MyBundle\Handler\FingersCrossed;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy as BaseErrorLevelActivationStrategy;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Activation strategy that ignores client errors (4xx)
*/
class ErrorLevelActivationStrategy extends BaseErrorLevelActivationStrategy
{
protected $requestStack;
public function __construct(RequestStack $requestStack, $actionLevel)
{
parent::__construct($actionLevel);
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function isHandlerActivated(array $record)
{
$isActivated = parent::isHandlerActivated($record);
if (
$isActivated
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
&& $record['context']['exception']->getStatusCode() >= 400
&& $record['context']['exception']->getStatusCode() <= 499
&& ($request = $this->requestStack->getMasterRequest())
) {
$isActivated = false;
}
return $isActivated;
}
}
https://gist.github.com/sobstel/d791d0347ee1f4e47b6e