Skip to main content

Distinguish new vs existing exceptions



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:





  1. Message (ie: Attempted to divide by zero.)



  2. Stack trace (ie: at componentNETapp.Form1.btnTrackExceptionsUn_Click(Object sender, EventArgs e) ...)



  3. Source (ie: componentNETapp)



  4. Target site (ie: Void btnTrackExceptionsUn_Click(System.Object, System.EventArgs))







Thanks


Comments

  1. First of all Errors or Exceptions? PHP differs between errors and exceptions. Generally the Error Handling and Logging­Docs section of the manual might be of interest.

    For Errors:

    error_get_last­Docs 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_handler­Docs 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_backtrace­Docs which can add even more context.

    For Exceptions:

    Each Exception­Docs 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_handler­Docs.



    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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Wildcards in a hosts file

I want to setup my local development machine so that any requests for *.local are redirected to localhost . The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local , site2.local etc, and have them all resolve to localhost , while Apache serves a different site accordingly.