Last active
March 21, 2016 09:43
-
-
Save sobstel/d791d0347ee1f4e47b6e to your computer and use it in GitHub Desktop.
Symfony2: exclude 4xx client errors from logging
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 (log) Configuration | |
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% | |
file: | |
type: stream | |
path: "%kernel.logs_dir%/%kernel.environment%.log" | |
level: info | |
console: | |
type: console | |
bubble: false |
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 MyBundle\Handler\FingersCrossed; | |
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy as BaseErrorLevelActivationStrategy; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
/** | |
* Activation strategy that ignores client errors (4xx) | |
*/ | |
class ErrorLevelActivationStrategy extends BaseErrorLevelActivationStrategy | |
{ | |
/** | |
* {@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 | |
) { | |
$isActivated = false; | |
} | |
return $isActivated; | |
} | |
} |
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
services: | |
mybundle.monolog.fingers_crossed.activation_strategy: | |
class: MyBundle\Handler\FingersCrossed\ErrorLevelActivationStrategy | |
arguments: ['error'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment