Good Way to Email Exceptions - php

I am running a site on Ubuntu with Apache and using PHP and Zend Framework.
I would like exception information emailed to the devs and am wondering about a good way to do this. I don't want to email every single exception right away because if something major happens, our inboxes will get flooded.
Instead, I am looking for a way that the exceptions and errors from the past hour can be emailed all at once (up to a certain size limit). I am thinking about writing a cron script to parse Apache's error_log but perhaps there are easier ways than doing that.

I'd recommend Hoptoad: http://hoptoadapp.com/pages/home

I work on a open source project. It's a ticket tracker that can receive error reports from any other PHP app, can detect duplicates to avoid email floods and email developers.
Look at http://elastik.sf.net/ and the "ErrorReportingService" module.
Version 0.3.1 is coming in several days with big improvements to the error collecting mechanisms.
Sample of an error report is at http://jarofgreen.wordpress.com/2011/01/30/tracking-errors-with-php/

If you're not interested in hosted solutions, and already using the Zend Framework, it shouldn't be too hard to write the errors to a special database or log, and have a periodically run process send the aggregated information.
As an example, my dayjob has an app that does this in a most stripped-down way: We use an extremely basic log (much like the apache logs), and a periodic process gets the content of the log, emails it, and truncates the file so that no old entries will be sent next time.
Of course, depending on how robust a solution you're looking for, you may want to go another route.

I just give some links which I think should be useful.
error_log: write errors to log and then let cron send them using email.
set_exception_handler:
Sets the default exception handler if
an exception is not caught within a
try/catch block. Execution will stop
after the exception_handler is called.
set_error_handler:
Sets a user function (error_handler)
to handle errors in a script.
Exceptions in PHP - Try/Catch or set_exception_handler??
http://www.slideshare.net/ZendCon/elegant-ways-of-handling-php-errors-and-exceptions-presentation

Related

Adding log to your code : The beginning

I have a website working and I would like to add log to be able to monitor if some errors append during the day.
I've search on stackoverflow and on the internet about it and there is a lot of information about logging framework, what to log... but for a beginner it is confusing. I do not know how to start.
Here are my questions :
If I had no info, I would create a .txt file and use file_put_contents in my PHP code, inside the catch {}. Is this solution possible ? If not, why? If yes, why is everybody using framework?
If not, what would you recommend ?
How do you use a log file. Do you monitor it once a day, twice a week ?
Where will be located this log file on sever ? In the www (public) directory ? or elsewhere?
If I had no info, I would create a .txt file and use file_put_contents
in my PHP code, inside the catch {}. Is this solution possible ? If
not, why?
You basically have three options here:
Send an email to a specific email address every time an error ocures.
Gather error messages in a logfile as you mentioned.
Create a database and log your error messages in there. You could have different severity levels and can build an extensive logging backend if you wish.
Personally it depends on the scale of your application. For small and medium web pages I think a simple logfile would be sufficient. But if you have lots of different errors and are willing to put some effort in, I can see the benefits of a database solution, especially the reporting on your errors can be a helpful tool.
If yes, why is everybody using framework?
A framework can help you, so that you don't have to code your error management from scratch.
How do you use a log file. Do you monitor it once a day, twice a week ?
There is no hard truth here. I would probably use two apporaches at the same time. I'd create a cronjob which gathers the recent errors and email those once a week to a specific email address. The recipient has to go through the errors (maybe just a summary) and will check if there is anything out of the ordinary. I would also implement a service that monitors your database/log file and create an alert (in form of an email for example) if there is a peek in error messages that is unusual. That way you can easily monitor error peaks.
Where will be located this log file on sever ? In the www (public) directory ? or elsewhere?
I would host them on the webserver but not in a public directory.
Your php.ini will have a log file specified where PHP will log errors. Your web server, such as Apache, will also have its own log file to record access and error. It is completely OK to have your own application log that will record abnormalities you observe in general operations.
Yes, it is fine to have a text file that will log business process abnormalities. I'd recommend having a standard for logging. Line would start with a timestamp, include [ERROR], [WARNING] or [INFO] messages so that logs can be parsed for type or error and between X and Y times. Frameworks are being used because they have a lot of built-in methods (scaffolding) already done so you don't have to write code from scratch. Frameworks also make it easier for other developers to hop on-board and start developing in a standard way. Take a look at this answer about why use a framework.
If a framework has a default location where log files go, I'd use that as long as the log file is not exposed to the public
Monitoring/Audit of a log file is different from logging. Depending on the critical nature of business abnormalities, I'd do auditing. For example, if the code is logging a warning that customer is attempting to order a particular part in mass, I'd monitor for WARNING messages every hour. I'd monitor for ERRORS every half hour (again, depending on the critical nature of the error). I'd prefer an overnight audit to see what all went wrong. More importantly, it would be ideal to have someone take charge of attending to those issues. You may find that some WARNINGS and ERRORS are not as important or relevant anymore. In those cases, someone should pick up the task of improving code to remove such logs. That way your logs say relevant and easy to attend to
The file should be located outside of the www public directory and should not be exposed through a URL. I prefer logs to be stored on a different drive altogether.
Personally I use this function
error_log();
I can then log any manual errors, along with other things such as index not found or parse errors, etc (which should not be occurring regularly). This function will assume the error log is wherever it's specified in php.ini

Error logging/handling on application basis?

We have a web server that we're about to launch a number of applications on. On the server-level we have managed to work out the error handling with the help of Hyperic to notify the person who is in charge in the event of a database/memcached server is going down.
However, we are still in the need of handling those eventual error and log events that happen on application level to improve the applications for our customers, before the customers notices.
So, what's then a good solution to do this?
Utilizing PHP:s own error log would quickly become cloggered if we would run a big number of applications at the same time. It's probably isn't the best option if you like structure.
One idea is to build a off-site lightweight error-handling application that has a REST/JSON API that receives encrypted and serialized arrays of error messages and stores them into a database. Maybe it could, depending on the severity of the error also be directly inputted into our bug tracker.
Could be a few well spent hours, but it seems like a quite fragile solution and I am sure that there's better more-reliable alternatives out there already.
Thanks,
Why would using PHP's own error_log quickly become 'cloggered'? If all goes well you won't see many errors, correct?
Building a separate application, especially one using an API, just for error reporting adds a lot of possible points of failure for error logging if you ask me. You could perhaps build an application that checks the existing error logs on various servers / for different applications to be able to display them in a sort of error dashboard, so you can see when things are REALLY going wrong.
But I'm interested to see what others might suggest as well, I haven't thought about it thát much yet for myself.
We actually tail -f the server's php error log for various outputs, but also catch a lot of exceptions we throw ourselves. By throwing a custom exception, you could have it (based on priority of course) write to your database, a log stream, your bug tracker and/or mail those-responsible-for-that-module. Of course you should log why the exception was raised (for example, var_export()/serialize() the func_get_args() of the method you threw an exception in. Make the exception message huge, since it will save you.
In addition to this, we use Zend_Log where exceptions are a bit overkill (for example if the argument given to the method should be deprecated, we might log a bit of debug_backtrace() to see where that call came from. This could be extended to programs making graphs of expensive calls etc, but that's a sidenote :)
My best tip: know where your application could fail and where it can not, it's so much easier to search for that error. Make sure errors that are raised based on external services are interpreted as such.
... and to be honest, I think this kind of thinking (Error-API/-Service on app. level) is a bit weird: how could you prevent an error if you knew exactly how to handle it? Wouldn't you avoid/automatize it?
You could use set_exception_handler and set_error_handler to gather more specific info and report it in a way that will alleviate the 'cloggering'. You could do different files for each client or application, introduce your own codes that allow for easy processing by an error parsing script, etc.
I'd be careful about introducing extra code or infrastructure to the error-handling process. It needs to be super solid so you can trust the info you get. Reporting errors directly to the database? What about database connection errors? (and on and on)
What you're searching for are PHPs error handling functions: http://de.php.net/manual/en/ref.errorfunc.php
Especially interesting would be set_error_handler() that allows you to completely override PHPs internal error handler.
With that you can make your own logs per application / send emails to admins / save error info to a db / tell users that an admin has been notified or whatever.
By either returning true or false you can also control wether PHPs internal error handler should run after your function or not.

Best Practices for Live Website Error Management

I am just about to launch a fairly large website for the first time. I have turned off all error messages in my php.ini and error messages are now logged to an "error_log" file on my server.
My question is, now that the errors are logged to a file, what are the best ways that web developers keep on top of seeing when/where errors occur on the website?
At the moment, it seems like the best way would be to constantly check the error_log file everyday, however this doesn't seem like the most efficient solution. Ideally I would receive an email everytime an error occurs (with the error message). Any advice on how I can keep on top of errors would be greatly appreciated!
Extra Info
Running on Shared Server (HostMonster)
Website Authored in PHP
There are two main functions in PHP that help catching errors and exceptions. I suggest that you take a look at them :
set_exception_handler
set_error_handler
In our company, we handle all errors that occurs on our websites with those functions, defining our own errors and exceptions handling methods.
When an error occurs, an email is sent to the developers team.
The place I previously worked at used a custom extension to handle error logging. It basically INSERT DELAY the errors into a DB with some extra information. Then, a separate admin tool was written to be able to easily search, browse, sort and manually prune the log table.
I recommend that you don't write a custom extension, but that you use the set_error_handler method and just write to a DB instead. If the DB is unavailable, then write to a file as a backup. It'll be worlds easier than dealing with a huge file and a one-off format.
If you want, you can also email yourself hourly summaries, but I don't suggest you send anything more than that or you'll be hating yourself.
You can email yourself on errors, if there was no email in last N hours.
If you don't expect many errors, a "private" RSS/ATOM feed might work well... whereby you don't need to worry if you don't get anything... but if you start getting "updates" you know there are issues.
I don't know how Hostmonster handles log rotation, but generally you want to monitor the size of your error_log file. If the size jumps suddenly, there's definitely something you need to check up on so you'ld want to get an email telling you that the logfile size jumped unexpectedly.
Other than that, you can combine the error logs at the end of the week and email them to yourself and debug on the weekend. If an error is only happening a few times a week it's probably not too serious of an issue.

What is the best way to get the errors from a production site in PHP?

For most production sites, you want to know when there has been an error as soon as possible. My question is how best to get this information.
Usually, it's probably best to get the errors in an email as I'm not going to sit every day and watch error logs until there is an error--this would be impossible since I have 20 or more production sites on different servers. These errors could be anything including, unset variables, invalid data received, or query errors.
At the moment I have followed the example on PHPs websites, found here. As a result, it creates a text string along with an XML file that is then sent by email. I have modified this slightly to keep all of the errors until the script ends and then send an email with the XML files attached. (I have crashed a couple mail servers sending over >500 000 emails because of an error in a loop.) Most of the time this works perfectly. (I have also created an object to do all of the error handling.)
The problem arises when there is a large amount of data for wddx_serialize_value() to processs. And then if there are multiple errors, then, it really ends up using a lot of memory, most of the time more than the script is allowed to use.
Because of this, I have added an addition gzcompress() to the XML file before storing it within the variable. This helps, but if the amount of data is very large, it still runs out of memory. (In a recent case it wanted to use around 2GB.)
I'm wondering what other solutions there are to this or how you have modified this to make it work?
So a few requirements:
it must be able to send more than just the error message to me and shouldn't make me login to the server to figure out what happened (so I can check when mobile and determine if it's an urgent matter)
there needs to be a limit on the number of emails sent. The best is still 1.
it needs to log to a file as per normal
Edit: I need other information related to the error, not just the error string. Often I find it's near to impossible to reproduce the error because it's caused by user input, which I don't know unless I get more information. I have tried my best to put in informative errors, but you never know how a user is going to use the system or what crap data they are going to put in. Therefore, I need more than just the error text/string.
Edit 2: Can't log errors to the database because for all I know the database may not be there. Need something that is pretty much guaranteed to run. Also, the websites are not all on 1 server and I often don't have access to cron on the server (stupid hosting companies).
Instead of setting a custom error handler, I let the errors go to the error log as usual. I set up a cron that runs periodically and monitors changes in the error log - if it changed, it sends me an email with the changes only. You can improve this process and parse the changes to better suit your needs - for example send you only errors above a certain level (such as E_WARNING and above).
One approach could be proper exception management in your application, i.e. to have control over which errors get logged.
Each raised exception would log the error details in a database.
Then, you could code a little application in order to search the error database, maybe just one for all your websites.
That way you avoid large unreadable log files, because everything is indexed and quickly searchable.
When your database gets too large, you can truncate your log tables via cron jobs.
Anacron, a cron job that emails changes to the error log* and an error log file should suffice.
The cron job can do all the processing required before sending the email.
One thing I have used in the past is epylog, it is a very flexible log monitoring app written in python. You can set it up to monitor your error logs and include the errors (or parts of them) in a log summary that is emailed to you.
I'd lean towards storing the more detailed error data in a flat file on the server and sending you an email to tell you to check the log. A cron job that watches the error directory or files for changes and has a rate limit set would be a good way to minimize impact on your running application.

How do I log uncaught exceptions in PHP?

I've found out how to convert errors into exceptions, and I display them nicely if they aren't caught, but I don't know how to log them in a useful way. Simply writing them to a file won't be useful, will it? And would you risk accessing a database, when you don't know what caused the exception yet?
You could use set_error_handler to set a custom exception to log your errors. I'd personally consider storing them in the database as the default Exception handler's backtrace can provide information on what caused it - this of course won't be possible if the database handler triggered the exception however.
You could also use error_log to log your errors. It has a choice of message destinations including:
Quoted from error_log
PHP's system logger, using the Operating System's system logging mechanism or a file, depending on what the error_log configuration directive is set to. This is the default option.
Sent by email to the address in the destination parameter. This is the only message type where the fourth parameter, extra_headers is used.
Appended to the file destination . A newline is not automatically added to the end of the message string.
Edit: Does markdown have a noparse tag for underscores?
I really like log4php for logging, even though it's not yet out of the incubator. I use log4net in just about everything, and have found the style quite natural for me.
With regard to system crashes, you can log the error to multiple destinations (e.g., have appenders whose threshold is CRITICAL or ERROR that only come into play when things go wrong). I'm not sure how fail-safe the existing appenders are--if the database is down, how does that appender fail?--but you could quite easily write your own appender that will fail gracefully if it's unable to log.
Simply writing them to a file won't be useful, will it?
But of course it is - that's a great thing to do, much better than displaying them on the screen. You want to show the user a nice screen which says "Sorry, we goofed. Engineers have been notified. Go back and try again" and ABSOLUTELY NO TECHNICAL DETAIL, because to do so would be a security risk. You can send an email to a shared mailbox and log the exception to file or DB for review later. This would be a best-practice.
I'd write them to a file - and maybe set a monitoring system up to check for changes to the filesize or last-modified date. Webmin is one easy way, but there are more complete software solutions.
If you know its a one-off error, then emailing a notice can be fine. However, with a many hits per minute website, do not ever email a notification. I've seen a website brought down by having hundreds of emails per minute being generated to say that the system could not connect to the database. The fact that it also had a LoadAvg of > 200 because of of the mail server being run for every new message, did not help at all. In that instance - the best scenario was, by far and away, the watchdog checking for filesizes and connecting to an external service to send an SMS (maybe an IM), or having an external system look on a webpage for an error message (which doesn't have to be visible on screen - it can be in a HTML comment).
I think it depends a lot of where your error occured. If the DB is down logging it to the DB is no good idea ;)
I use the syslog() function for logging the error, but I have no problems writing it to a file when I'm on a system which has no syslog-support. You can easily set up your system to send you an email or a jabber message using for example logwatch or the standard syslogd.
I second log4php. I typically have it configured to send things like exceptions to ERROR or CRITITCAL and have them written to syslog. From there, you can have your syslog feed into Zenoss, Nagios, Splunk or anything else that syslog can talk to.
You can also catch and record PHP exceptions using Google Forms. There is a tutorial here that explains the process.

Categories