I am creating a PHP application and I want to display the number of times an error has occurred. The problem that I am trying to figure out is if an error has already been reported or if its new using the following values:
- Message (ie: Attempted to divide by zero.)
- Stack trace (ie: at componentNETapp.Form1.btnTrackExceptionsUn_Click(Object sender, EventArgs e) ...)
- Source (ie: componentNETapp)
- Target site (ie: Void btnTrackExceptionsUn_Click(System.Object, System.EventArgs))
Thanks
First of all Errors or Exceptions? PHP differs between errors and exceptions. Generally the Error Handling and LoggingDocs section of the manual might be of interest.
ReplyDeleteFor Errors:
error_get_lastDocs will return the last error with the following information: Type (integer), Message (string), File (string) and Line (integer). I would say this is self explanatory. You might also want to look into set_error_handlerDocs which will - next to these four bits of information - also offer you an error context you could make use of.
If you need more information about an error, there is also the backtrace in PHP debug_backtraceDocs which can add even more context.
For Exceptions:
Each ExceptionDocs has similar members: Message (string), Code (integer), File (string) and Line (integer). It also has a Trace which contains more information and could be used as context. It's also possible to set a previous exception to nest exceptions. Like the the error handler, it's possible to use an exception handler as well: set_exception_handlerDocs.
With the information given, you should be able to handle and log all errors/exceptions in a manner that you can analyse them later on. Next to that it's also possible to make use of PHP error logging and to store those errors into a database. See Outputting all PHP errors to database not error_log for a simple example. I would say the key point is that you store the error information in a more or less normalized form, so that you can define criteria later on how to count/group them.
If you're looking for more infrastructure, take a look into the apache logging services project that offers some tools (e.g. a viewer) and a PHP logger exists for it, too (log4php).
But this is just exemplary, other tools exist as well, some of them are using a MySQL backend and you can not only track PHP but also other errors.