Giter Site home page Giter Site logo

fperrornotifierplugin's Introduction

#fpErrorNotifierPlugin

Overview

The plugin for those people how want to feel confident in your code. If something goes wrong you are the first to be notified about it. The email not only contains an error message but a bunch of useful information.

It takes control over the system and catches every error: exceptions, fatal errors, notices, memory limit error, php parse errors and so on. It easy to customize because the plugin was made as a set of components: message, driver, handler, decorator.

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 general way of useing the plugin is to send emails about each error has 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 earlier and it 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:                   '[email protected],[email protected],[email protected]'
      from:                 '[email protected]'

That's it. Now we have all errors and exceptions caught and sent 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
      ignore_duplication:      true
      ignore_duplication_time: 10 # seconds

You can avoid sending duplicated errors for some period of time. Ignore some php errors or exception. Also it is possible to get notifications that happend under the '@' command.

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:                   '[email protected],[email protected],[email protected]'
      from:                 '[email protected]'
  • 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:                   '[email protected],[email protected],[email protected]'
      from:                 '[email protected]'

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:   [email protected]
        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);

But this code creates a hard coded relation between your code and the plugin isn't it? It can be done this way but it is not a good idea. So how can we do it better? Below we are sending absolutely the same message using sfEventDispatcher:

<?php

$dispatcher = sfContext::getInstance()->getEventDispatcher();
$event = new sfEvent('A Custom message title', 'notify.send_message', array('Detail 1' => 'Foo', 'Detail 2' => 'Bar'));

$dispatcher->notify($event);

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'));
}

// notify.decorate_message for adding additional info to custom simple messages
fpErrorNotifier::getInstance()->dispather()->connect('notify.decorate_exception', 'addMoreErrorInfo');

// then when an error happend this event would be raised and additional info added.

Use custom driver

<?php 

$driver = new sfErrorNotifierDriverMailNative(array(
  'to' => '[email protected]',
  'from,' => '[email protected]'));

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.

fperrornotifierplugin's People

Contributors

ksn135 avatar makasim avatar orthographic-pedant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fperrornotifierplugin's Issues

More flexible ignore handler

The more flexible ignore handler.

It would be cool to have ability to define ignored erros by minix this options:

  • message pattern
  • exception class for php errors it will be a ErrorException instance or its children.
  • code matching
  • severities matching
  • and so on

Assigned to 66Ton99

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.