![]() |
|
fpErrorNotifierPlugin - 1.0.3Allows to catch all kind of exceptions\errors (including fatal and parse) and store them to file or send via email. |
|
It catches all kinds of errors like: exceptions, fatal errors, notices, memory limit errors and so on. It is very useful for production servers. You will find out about any errors in a moment after it has happened and not when an angry customer calls 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 reason to use is that it was made as a set of components: message, driver, handler, decorator. These components are easy to changes or extend (in most cases just by modifing notify.yml config)
php symfony plugin:install fpErrorNotifierPlugin
git clone git://github.com/makasim/fpErrorNotifierPlugin.git
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
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins('fpErrorNotifierPlugin');
}
}
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: 'manager@example.com,teamleader@example.com,developer@example.com'
from: 'noreply@live.example.com'
That's it. Now we have all errors and exceptions caught and sent to the development team members.
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:
There are two handlers which comes with the plugin:
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
There are four drivers comes with the plugin:
notify.yml
all:
driver:
class: fpErrorNotifierDriverMailNative
options:
to: 'manager@example.com,teamleader@example.com,developer@example.com'
from: 'noreply@live.example.com'
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'
notify.yml
driver:
class: sfErrorNotifierDriverFile
options:
path: '%SF_LOG_DIR%/last-error.html'
You can render the message as simple text or html (set by default).
notify.yml
all:
decorator:
class: fpErrorNotifierDecoratorHtml
options: {}
notify.yml
all:
decorator:
class: fpErrorNotifierDecoratorText
options: {}
<?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);
<?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.
<?php
$driver = new sfErrorNotifierDriverMailNative(array(
'to' => 'first.developer@example.com',
'from,' => 'noreplay@yout-project.com'));
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
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.