I am using Monolog
in a Symfony 2.8
project. I have configured a mail handler to send me notification about all errors.
Since not all errors are important (e.g. PageNotFound
for /apple-app-site-association
) I would like to add a filter. I did this by adding a custom Handler:
Config:
services:
monolog.custom_handler.service:
class: AppBundle\Log\CustomHandler
monolog:
main:
type: service
id: monolog.custom_handler.service
level: error
handler: mail_buffer
mail_buffer:
type: buffer
handler: mailer
mailer:
type: swift_mailer
from_email: "my address"
to_email: "my address"
subject: ERROR!
formatter: monolog.formatter.extended
Handler:
namespace AppBundle\Log;
use Monolog\Handler\AbstractHandler;
class CustomHandler extends AbstractHandler {
public function handle(array $record) {
// Check if handling level was reached
if ($record['level'] < $this->level)
return false;
if (!empty($record)) {
$url = (isset($record['extra']) && isset($record['extra']['url']) ? $record['extra']['url'] : '');
$message = (isset($record['message']) ? $record['message'] : '');
// Well Known Pages
if (strpos($url, '/.well-known') === 0) {
//echo "...match 1</br>";
return true;
}
if ($url == '/apple-app-site-association') {
//echo "...match 2</br>";
return true;
}
if (strpos($url, '/apple-touch-icon') === 0) {
//echo "...match 3</br>";
return true;
}
...
}
// Record was NOT handled --> DO NOT stop bubbeling
return false;
}
}
Using the echo
statements I can confirm, that the handler is called, and the if
clause correctly matches. So, if /apple-app-site-association
is called for example, the handler returns true
. I would expect, that this stops bubbeling, and that thus the nested mail_buffer
handler is NOT called anymore. This is not the case.
No matter if the handler return true or false, I still receive mail for all error.
What am I doing wrong? How can I stop the processing of filtered messages?