Zend Framework Exception Notifier

Do you want to be notified when an exception is raised in your code? This code is for you. I wrote an application resource that will send you a nicely formatted email with all the data about the exception. In order to use it you have to install my library and insert the following lines into your application.ini file

autoloadernamespaces[] = "Zle_"
pluginPaths.Zle_Application_Resource = "Zle/Application/Resource"

Then you’ll have to setup the application resource using the following directives

; notifier settings
resources.notifier.project = Project Name
resources.notifier.addresses[] = foo@example.com
resources.notifier.addresses[] = bar@example.com

This snippet of code attachs an email log writer to the log resource, so don’t forget to configure the log resource in application.ini file. The generated email will be sent using the default transport, so you’ll have to configure it otherwise the default transport (sendmail) would be used.

The default error controller will log any error with the CRIT priority, this will trigger the writer and send a message like this one to the given recipients:

exception notification

You would like to avoid to receive notifications for a page not found (otherwise all the request generated by script kiddies would flood your inbox), in order to do this you can edit the default error controller to keep track of that in this way:

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        $logError = true;
        // skipped code
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $logError = false;
        // other case for the switch
        // Log exception, if logger available
        if ($logError && $log = $this->getLog()) {
            $log->crit($this->view->message, $errors->exception);
        }

Last but not least, when you are in development or in testing mode you can avoid notifications by using this line in the right section of your application.ini

; disable notifier for development
resources.notifier.disabled = true
You can leave a response, or trackback from your own site.
  • Ezo

    Hello, Thanks for your code contribution and tutorial.
    Unfortunately i could not get it working yet.
    In my error controller the call to $this->getLog() throws an exception (the method getLog is unknown).
    I am new with the use of resources, so maybe i forget to initialize the Zle_Application_Resource_Notifier?

    Help is much appriciated.

  • http://www.codewithstyle.eu Fabio

    getLog() is a method defined by the default ErrorController generated in a new zend framework project. If you don’t have the standard error controller you can always create the getLog method. You can find its code here https://gist.github.com/1579905

  • Ezo

    Thanks that did the trick :)
    I like how you use a lot of the build in ZF parts.
    I will try to alter your code now, to print instead of email an exception, incase of a development environment or active user.

Subscribe to RSS Feed