14

How do I setup email notifications for exceptions that are either logged or thrown on the site?

UPDATE: A few people have commented on the fact that you may expect to get way too many emails if you have every exception emailed to you. I tend to like to keep my exception log pretty light. Anything that goes in there I view as, well, an exception. If it's expected functionality and not a problem, then I like to catch the exception, possibly log it to another file (maybe system.log) if needed, but not log it to exception.log.

But if you have a lot of noise in your exception.log that you don't want to clean, you're likely not going to want to do this.

1
  • 7
    YOU WILL CRASH GMAIL
    – benmarks
    Jun 18, 2013 at 23:08

6 Answers 6

4

You can use the Magento Hackthon Logger for this job: https://github.com/firegento/firegento-logger/

It is not the question, but there are extensions for the extension: https://github.com/magento-hackathon/LoggerSentry/

What I want to say: It is easy to implement your own "Writer" :-)

4
  • Nice! Thanks Paul. Would that allow you to email yourself only exception logs? That was the primary use case I was interested in and I think something that could be generally applicable to many Magento installs. Jun 18, 2013 at 21:53
  • You can set up various treshholds what to do with logs, send Exceptions to XMPP (gmail chat), send errors to mail and log the notices to the database for example. Jun 20, 2013 at 13:05
  • Just FYI, this project has been moved... github.com/firegento/firegento-logger Oct 7, 2014 at 13:53
  • @PaulHachmang the first link github.com/magento-hackathon/Logger is dead. May want to update it.
    – SR_Magento
    Jun 23, 2015 at 16:10
8

This didn't seem as readily accessible as I thought it should be from some searching I did, so I'm posting up a question question/answer for future reference.

Thrown Exceptions

You can enable email notifications for exceptions that are thrown using errors/local.xml. You can copy your errors/local.xml.template to `errors/local.xml' and drop in the email address and subject line that you want to use.

<config>
    <skin>default</skin>
    <report>
        <action>email</action>
        <subject>domain.com exception</subject>
        <email_address>[email protected]</email_address>
        <trash>leave</trash>
    </report>
</config>

Then, if any exceptions are thrown and caught by Magento's error handling system, where an error report screen is displayed to the end user, you'll get an email notification.

Logged Exceptions

If you would also like to get email notifications for exceptions that aren't thrown but rather are logged, you'll need to get a little bit further under the hood.

First, override the core log writer class.

Once you've done that, just drop in the code to shoot yourself an email after the exception is logged. Remember that you need to save the log filename from within the __construct() in order to have access to it from the _write() method.

class Module_Core_Model_Zend_Log_Writer_Stream extends Zend_Log_Writer_Stream
{
    protected $_streamOrUrl;

    public function __construct($streamOrUrl, $mode = NULL)
    {
        parent::__construct($streamOrUrl, $mode);

        $this->_streamOrUrl = $streamOrUrl;
    }

    protected function _write($event)
    {
        parent::_write($event);

        if (strpos($this->_streamOrUrl, 'exception.log') === false) {
            return;
        }

        // Send email here    
    }
}
3
  • Be careful of this. If something goes wrong on the site, you will end up with a flood of emails. This in itself would drag the server itself down. You then end up with not just a broken site, but an inaccessable server! You then end up having to also clear a lot of mails from the mail que. This happened to me in the past
    – ProxiBlue
    Jun 19, 2013 at 6:56
  • That's a good point. We're not managing the mail server ourselves (Mandrill) but still things could get clogged up quickly if all of a sudden exceptions started flooding for some reason. Jun 20, 2013 at 0:25
  • I always thought a mail que would be a great module. (any takers for one hosted on github/know of one?) - basically any emails magento send to be dropped into a mail que, with a cron that runs every x minutes and sends the next email in the que. The que could have a counter and if an identical message is placed, it could just up the counter. Thus if you have 100 exception messages, from the same exception, you will only get 1 email, with a 'xxx instances of this email'. It would also give a great point to check if emails were sent from magento. - I just don't have the time to write this one :(
    – ProxiBlue
    Jun 20, 2013 at 3:47
8

Emailing every exception will probably result in a LOT of mail.

It might be a better option to just log the exceptions to the exception log and mail that once a day to yourself using a cronjob in linux

0 23 * * * /usr/lib/sendmail [email protected] < /home/shop.com/www/var/log/exception.log

And optionally cleaning it up after you've mailed it

0 23 * * * /usr/lib/sendmail [email protected] < /home/shop.com/www/var/log/exception.log;rm /home/shop.com/www/var/log/exception.log

Or you could execute it every hour if you need a higher frequency.

This way there is no need to overwrite any files and keeping the load on the server that constantly sending mails to yourself down.

1
  • Thanks Sander. I would argue though that exceptions that are logging are a problem that need to be resolved, so you should have a low occurrence of exceptions being logged. I do like the simplicity of a cron job to pipe the exception log, but I'm not sure that I like the idea of clearing out the log every time it's emailed. Jun 18, 2013 at 21:51
2

maybe you can also be interested in my Magento/Monolog integration

https://github.com/aleron75/magemonolog

Regards Alessandro

2
  • Nice job done, just want to know if it is tested on magento version 1.7? It doesn't sends email for me on magneto version 1.7.0.2
    – Haris
    Oct 7, 2016 at 16:01
  • 1
    Sorry @Haris I don't know but unsent e-mail likely depends on your email server configuration rather than on the extension Oct 8, 2016 at 16:50
1

I dont like the implementation of Mage::log's custom writerModel or I dont understand it. For me it does not take advantage of Zend_Log design to enable n writers. I would have taken a Log not a Writer. Therefore I made a workaround to take the full advantage of the Zend_Log and still write not much code, since I aggregate a Zend_log.

class XX_XXX_Model_Log extends Zend_Log_Writer_Abstract{

/**
 * @var Zend_Log
 */
protected $zendLog = array();

/**
 *
 * @param  mixed $writer Zend_Log_Writer_Abstract or Config array
 * @return void
 */
public function addWriter($writer)
{
    return $this->zendLog->addWriter($writer);
}

/**
 * Write a message to the log.
 *
 * @param  array  $event  log data event
 * @return void
 */
protected function _write($event){
    $this->zendLog->log($event['message'], $event['priority']);
}

/**
 * Konstruktor erweitert Log automatisch um Writer
 *
 * @param sting $file
 */
public function __construct($file){
    $this->zendLog = new Zend_Log();
    $this->enhance($file);
}

/**
 * Hier werden die Writer and Zend_Log angehangen
 *
 * @param sting $file
 */
public function enhance($file){
    /* put your custom Logger here - example firePHP*/
        $writer = Mage::getModel('gdcore/firephp');
        $writer->addFilter(Zend_Log::Debug);
        $this->addWriter($writer);
    }
}

/**
 * Construct a Zend_Log driver
 * 
 * @param  array|Zen_Config $config
 * @return Zend_Log_FactoryInterface
 */
static public function factory($config){
    return Zend_Log::factory($config);
}
}

By the way in Magento 1.7 I get just a few emails. Each email has some exceptions/errors for one request, I guess. So highly frequented site will still result in many email. I will have look on that. By now its okay for me.

4
  • Thanks Andreas. I don't 100% understand the role of the writer model vs the log model myself either, but it seemed the most natural place to extend Magento in this instance. Aug 27, 2013 at 18:44
  • Please note XX_XXX_Model_Log is a writer_model! Just configure your config.xml and add a email-writer in method enhance to send some mails too. Perhaps we misunderstood. Aug 27, 2013 at 23:56
  • Sorry I meant writer model vs writer stream model. At any rate, the solution that I gave above works fine, and appears to be as readable as this and also has less SLOC. Aug 28, 2013 at 23:57
  • With this solution you can use the Zend Framework for Zend-logging and configure as many writers you want with filters and priority. So it is very easy to make a configurable (priority) further f.e. File-logging writer which you can send with a Mage cronjob every hour via mail. Aug 29, 2013 at 8:30
0

You also could use a module to create email notifications or get daily reports of customers which got exceptions:

Here is explained the implementation: https://grafzahl-io.blogspot.de/2017/03/notifications-for-every-exception-in-magento.html

This is the module to monitor errors and other events (even apply a discount to the customer if he got an exception): https://grafzahl.io/notify-module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.