From 05c9e996bc7e84ba20bc9ac268931abb1a5e6cd0 Mon Sep 17 00:00:00 2001 From: Karim Elshendy Date: Tue, 3 Nov 2015 13:10:43 +0200 Subject: [PATCH 1/2] adding ability to ignore user agents and urls using patterns --- DependencyInjection/Configuration.php | 6 ++++ Listener/Notifier.php | 48 +++++++++++++++++++-------- README.md | 25 +++++++++++++- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7f15a30..361ec5f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -76,6 +76,12 @@ public function getConfigTreeBuilder() ->end() ->treatNullLike(array()) ->end() + ->scalarNode('ignoredAgentsPattern') + ->defaultValue('') + ->end() + ->scalarNode('ignoredUrlsPattern') + ->defaultValue('') + ->end() ->end(); return $treeBuilder; diff --git a/Listener/Notifier.php b/Listener/Notifier.php index 0b72c4f..644b3bb 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -47,6 +47,8 @@ class Notifier private $reportSilent = false; private $repeatTimeout = false; private $ignoredIPs; + private $ignoredAgentsPattern; + private $ignoredUrlsPattern; private $command; private $commandInput; @@ -62,20 +64,22 @@ class Notifier */ public function __construct(Swift_Mailer $mailer, EngineInterface $templating, $cacheDir, $config) { - $this->mailer = $mailer; - $this->templating = $templating; - $this->from = $config['from']; - $this->to = $config['to']; - $this->handle404 = $config['handle404']; - $this->handleHTTPcodes = $config['handleHTTPcodes']; - $this->reportErrors = $config['handlePHPErrors']; - $this->reportWarnings = $config['handlePHPWarnings']; - $this->reportSilent = $config['handleSilentErrors']; - $this->ignoredClasses = $config['ignoredClasses']; - $this->ignoredPhpErrors = $config['ignoredPhpErrors']; - $this->repeatTimeout = $config['repeatTimeout']; - $this->errorsDir = $cacheDir.'/errors'; - $this->ignoredIPs = $config['ignoredIPs']; + $this->mailer = $mailer; + $this->templating = $templating; + $this->from = $config['from']; + $this->to = $config['to']; + $this->handle404 = $config['handle404']; + $this->handleHTTPcodes = $config['handleHTTPcodes']; + $this->reportErrors = $config['handlePHPErrors']; + $this->reportWarnings = $config['handlePHPWarnings']; + $this->reportSilent = $config['handleSilentErrors']; + $this->ignoredClasses = $config['ignoredClasses']; + $this->ignoredPhpErrors = $config['ignoredPhpErrors']; + $this->repeatTimeout = $config['repeatTimeout']; + $this->errorsDir = $cacheDir . '/errors'; + $this->ignoredIPs = $config['ignoredIPs']; + $this->ignoredAgentsPattern = $config['ignoredAgentsPattern']; + $this->ignoredUrlsPattern = $config['ignoredUrlsPattern']; if (!is_dir($this->errorsDir)) { mkdir($this->errorsDir); @@ -100,6 +104,22 @@ public function onKernelException(GetResponseForExceptionEvent $event) return; } + if (strlen($this->ignoredAgentsPattern)) + { + if (preg_match('#'.$this->ignoredAgentsPattern.'#', $event->getRequest()->headers->get('User-Agent'))) + { + return; + } + } + + if (strlen($this->ignoredUrlsPattern)) + { + if (preg_match('#'.$this->ignoredUrlsPattern.'#', $event->getRequest()->getUri())) + { + return; + } + } + if (500 === $exception->getStatusCode() || (404 === $exception->getStatusCode() && true === $this->handle404) || (in_array($exception->getStatusCode(), $this->handleHTTPcodes))) { $this->createMailAndSend($exception, $event->getRequest()); } diff --git a/README.md b/README.md index bae9f03..41abab5 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ You may control the depth of recursion with a parameter, say foo = array('a'=>ar Default value is 1. (MAX_DEPTH const) -### How to ignore sending HTTP errors if request comes from given IPs ? +### How to ignore sending HTTP errors if request comes from any of given IPs ? If you want to ignore sending HTTP errors if the request comes from specific IPs, you can now specify the list of ignored IPs. @@ -166,6 +166,29 @@ elao_error_notifier: ignoredIPs: - "178.63.45.100" - ... + +### How to ignore sending HTTP errors if the user agent match a given pattern? + +For some reasons you may need to ignore sending notifications if request comes from some user agents. +Often you will need to use this feature with annoying crawlers which uses artificial intelligence +to generate URLs which may not exist in your site. + +```yml +# app/config/config_prod.yml +elao_error_notifier: + ignoredAgentsPattern: "(Googlebot|bingbot)" + +``` + +### How to ignore sending HTTP errors if the URI match a given pattern? + +For example if you want to ignore all not exist images errors you may do something like that. + +```yml +# app/config/config_prod.yml +elao_error_notifier: + ignoredUrlsPattern: "\.(jpg|png|gif)" + ``` ## Screenshot From 20a321ad92c61d5ac00f6989283ad96ee07836ff Mon Sep 17 00:00:00 2001 From: Karim Elshendy Date: Tue, 3 Nov 2015 15:46:55 +0200 Subject: [PATCH 2/2] fixing code style to follow PSR2 convention --- Listener/Notifier.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Listener/Notifier.php b/Listener/Notifier.php index 644b3bb..4103ccb 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -104,18 +104,14 @@ public function onKernelException(GetResponseForExceptionEvent $event) return; } - if (strlen($this->ignoredAgentsPattern)) - { - if (preg_match('#'.$this->ignoredAgentsPattern.'#', $event->getRequest()->headers->get('User-Agent'))) - { + if (strlen($this->ignoredAgentsPattern)) { + if (preg_match('#'.$this->ignoredAgentsPattern.'#', $event->getRequest()->headers->get('User-Agent'))) { return; } } - - if (strlen($this->ignoredUrlsPattern)) - { - if (preg_match('#'.$this->ignoredUrlsPattern.'#', $event->getRequest()->getUri())) - { + + if (strlen($this->ignoredUrlsPattern)) { + if (preg_match('#'.$this->ignoredUrlsPattern.'#', $event->getRequest()->getUri())) { return; } }