Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #50 from karim-elshendy/new_ignore_features
Browse files Browse the repository at this point in the history
adding ability to ignore user agents and urls using patterns
  • Loading branch information
benji07 committed Nov 5, 2015
2 parents 1a06387 + 20a321a commit a312af0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public function getConfigTreeBuilder()
->end()
->treatNullLike(array())
->end()
->scalarNode('ignoredAgentsPattern')
->defaultValue('')
->end()
->scalarNode('ignoredUrlsPattern')
->defaultValue('')
->end()
->end();

return $treeBuilder;
Expand Down
44 changes: 30 additions & 14 deletions Listener/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Notifier
private $reportSilent = false;
private $repeatTimeout = false;
private $ignoredIPs;
private $ignoredAgentsPattern;
private $ignoredUrlsPattern;
private $command;
private $commandInput;

Expand All @@ -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);
Expand All @@ -100,6 +104,18 @@ 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());
}
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down

0 comments on commit a312af0

Please sign in to comment.