Last active
August 8, 2023 09:38
-
-
Save simshaun/50b4b4c1a4dab9b4d34114592343689d to your computer and use it in GitHub Desktop.
Monolog activation strategy to ignore 403, 404, and 405 errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
monolog: | |
handlers: | |
main_error: | |
........ | |
activation_strategy: monolog.strategy.activation.limited_error | |
........ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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