0

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?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
  • Have you tried with setting up levels for handlers? – mmmm Oct 24 '16 at 10:15
  • I am not sure what you mean. The `main` handler is configured to level `error`. Thus all following handlers should only get messages of level `error` or height, shouldn't they? All `PageNotFound` errors are logged using the same level. So `/callToUnknownPage` has the same level as `/apple-app-site-association`, but the first should be logged while the second one should be skipped... – Andrei Herford Oct 24 '16 at 10:23
  • maybe other way - is it possible for You to inject there custom logger? Because if so then create other channel, custom handler for this channel that will send mails and you do not have to care about other errors. – mmmm Oct 24 '16 at 10:30
  • This question is not about creating Monolog channels or injecting custom loggers. Error like `PageNotFound` are thrown on many different places I cannot control. I cannot inject a custom logger into core classes or third party bundles. Thus applying a custom filter to the log messages is my only option. Thus, this question is about how to apply a custom filter to log messages... – Andrei Herford Oct 24 '16 at 10:33
  • ok, this is why I was asking :) honestly - haven't had this kind of issue. If I were You I would try to run xdebug and see what is exactly happening. Sorry for not direct question-resolver. – mmmm Oct 24 '16 at 10:47

0 Answers0