Instantly share code, notes, and snippets.

Embed
What would you like to do?
Symfony2: exclude 4xx client errors from logging
# 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
<?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;
}
}
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