fpErrorNotifierPlugin - 1.0.2

Allows to catch all kind of exceptions\errors (including fatal and parse) and store them to file or send via email

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

fpErrorNotifierPlugin

Overview

It caught all kind of errors like: exceptions, fatal errors, notices, memory limit errors and so on. It is very useful on production servers. You know about an error in a moment it has happen not when an engry customer call you. The mail will contain a lot of useful information like description stack trace, module\action, dump of $_SERVER and $_SESSION variables and so on.

Another good point of the plugin that it was made as set of components: message, driver, handler, decorator. Which are easy to changes or extend (in most cases just by modifing notify.yml)

Requirements

Installation

Download:

Pear package

php symfony plugin:install fpErrorNotifierPlugin

Git dev:

git clone git://github.com/makasim/fpErrorNotifierPlugin.git

Git tag:

git clone git://github.com/makasim/fpErrorNotifierPlugin.git 
cd fpErrorNotifierPlugin
git tag
# check out the latest tag - 1.0.0 for this example
git checkout 1.0.0

Enable it

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('fpErrorNotifierPlugin');
  }
}

Getting Started

The main useage of the plugin is to send email about every errors that have happend on the server (including exceptions, notice, fatal and so on). Let's look at this example to khow how to do it with this plugin.

First we need to install the plugin. It was described elier and is a common symfony plugin installation Second we have to create notify.yml in any of the project config dir (I will store it in SF_ROOT_DIR/config/notify.yml) with a next content:

notify.yml

all:
  driver:
    class:                  fpErrorNotifierDriverMailNative
    options:
      to:                   'manager@example.com,teamleader@example.com,developer@example.com'
      from:                 'noreply@live.example.com'

That's it. Now we have all erros and exception to be caught and send to the development team members.

Features

The notify.yml config

After you setup the plugin it starts to work. By default it logs the last error into a file in the log dir. To change this behavior you need to create notify.yml in project or app config folder.

So let's say I copy notify.yml from plugin's config directory to SF_ROOT_DIR/config/notify.yml

notify.yml

prod:

all:
  handler:
    class:                   fpErrorNotifierHandler
    options:                 {}

  message:
    class:                   fpErrorNotifierMessage
    options:                 {}

  helper: 
    class:                   fpErrorNotifierMessageHelper
    options:                 {}

  decorator:
    class:                   fpErrorNotifierDecoratorHtml
    options:                 {}

  driver: 
    class:                   fpErrorNotifierDriverNull
    options:                 {}

As you can see we have some stuff like handler, message, helper, decorator and driver:

  • Handler - it is a most valuable things. Because it cauth any errors and handle it
  • Message - is just a data container.
  • Helper - helps to fill the message with an information (like fill message from Exception instance).
  • Decorator - it wrapps the message and know hot the message can be rendered.
  • Driver - it is a object which knows where to send or store the message.

Handlers

There are two handlers which comes with the plugin:

  • fpErrorNotifierHandler - base implementation
  • fpErrorNotifierHandlerIgnore - extended version with some ignoring abilities.

fpErrorNotifierHandler does not take any options and can be configerd like this:

notify.yml

all:
  handler:
    class:                   fpErrorNotifierHandler
    options:                 {}

fpErrorNotifierHandlerIgnore:

notify.yml

all:
 handler:
   class:                   fpErrorNotifierHandlerIgnore
     options:
      ignore_@:              false
      ignore_errors:         [<?php echo E_ERROR ?>, <?php echo E_NOTICE ?>]
      ignore_exceptions:     [FooException]
      log_ignored:           true

Drivers

There are four drivers comes with the plugin:

  • fpErrorNotifierDriverMailNative - use php's mail function to send an email.

notify.yml

all:
  driver:
    class:                  fpErrorNotifierDriverMailNative
    options:
      to:                   'manager@example.com,teamleader@example.com,developer@example.com'
      from:                 'noreply@live.example.com'
  • fpErrorNotifierDriverMailSymfony - use a mailer (It should be Swift) configured via factories.yml. It is taken from sfContext.

notify.yml

all:
  driver:
    class:                  fpErrorNotifierDriverMailSymfony
    options:
      to:                   'manager@example.com,teamleader@example.com,developer@example.com'
      from:                 'noreply@live.example.com'

It is an example of SWIFT mailer configuration with gmail.com account

factories.yml

mailer:
  class: sfMailer
  param:
    logging:           %SF_LOGGING_ENABLED%
    charset:           %SF_CHARSET%
    delivery_strategy: realtime
    transport:
      class: Swift_SmtpTransport
      param:
        host:       smtp.gmail.com
        port:       587
        encryption: tls
        username:   your-account@gmail.com
        password:   'password'
  • fpErrorNotifierDriverFile - store the last error to the file (It can be helpfull for testing services in development process).

notify.yml

driver:
  class:             sfErrorNotifierDriverFile
    options:         
      path:          '%SF_LOG_DIR%/last-error.html'
  • fpErrorNotifierDriverNull - just does do nothing

Decorators

You can render the message as simple text or html (set by default).

  • fpErrorNotifierDecoratorHtml

notify.yml

all:
  decorator:
    class:                   fpErrorNotifierDecoratorHtml
    options:                 {}
  • fpErrorNotifierDecoratorText

notify.yml

all:
  decorator:
    class:                   fpErrorNotifierDecoratorText
    options:                 {}

Customizing

Send a custom message

<?php

$message = fpErrorNotifier::getInstance()->decoratedMessage('A Custom message title');
$message->addSection('Detailed info', array('Detail 1' => 'Foo', 'Detail 2' => 'Bar'));

fpErrorNotifier::getInstance()->driver()->notify($message);

Add more info to the error message

<?php 

function addMoreErrorInfo(sfEvent $event)
{
  $message = $event->getSubject();
  $message->addSection('Detailed info', array('Detail 1' => 'Foo', 'Detail 2' => 'Bar'));
}

fpErrorNotifier::getInstance()->dispather()->connect('notify.exception', 'addMoreErrorInfo');

// then whrn an error happend this event would be raised.

Use custom driver

<?php 

$driver = new sfErrorNotifierDriverMailNative(array(
  'to' => 'first.developer@example.com',
  'from,' => 'noreplay@yout-project.com'));

Run the plugin tests

It's used sfPhpunitPlugin as a testing framework.

So to run test you need this plugin first. Then you can run this command to execute the plugin tests.

./symfony phpunit --only-plugin=fpErrorNotifierPlugin

Feedback

I am very welcome for any comments suggestions, bug fixes, implementations and so on. You can create a ticket at my github repository or make a fork and do your changes.