Skip to content

Instantly share code, notes, and snippets.

@simshaun
Last active August 8, 2023 09:38
Show Gist options
  • Save simshaun/50b4b4c1a4dab9b4d34114592343689d to your computer and use it in GitHub Desktop.
Save simshaun/50b4b4c1a4dab9b4d34114592343689d to your computer and use it in GitHub Desktop.
Monolog activation strategy to ignore 403, 404, and 405 errors
monolog:
handlers:
main_error:
........
activation_strategy: monolog.strategy.activation.limited_error
........
<?php
namespace Acme\Monolog;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Symfony\Component\HttpKernel\Exception\HttpException;
class LimitedErrorActivationStrategy extends ErrorLevelActivationStrategy
{
public function __construct()
{
parent::__construct('error');
}
public function isHandlerActivated(array $record): bool
{
$isActivated = parent::isHandlerActivated($record);
if (
$isActivated
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
) {
return !in_array($record['context']['exception']->getStatusCode(), [403, 404, 405]);
}
return $isActivated;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment