Error Reporter: Example

The error reporter is meant to sit on top of your application. PHP throws errors and you usually just see the PHP warning messages at the top of the page, catch the line and reload. But when you are doing more intricate applications, this is often not a convenient way to debug. Usually errors are thrown inside of functions which take large arrays and the error might be a little undefined index error.

When this script is initialized, it begins trapping all the non-fatal errors which occur on the page. When the page is finished loading, the Reporter object iterates over all of the captured errors and performs the specified reports based on the error level. This example demonstrates the 'console' report, which is a secondary window which can be openned to view the errors, source code and context at the time the error was thrown. (optional reports include email, file, system, browser and stdout, each to be used in the appropriate environment).

Each error report can take a minimum level onto which to act. This can be nice for a "live" system to send e-mail reports when a critical error occurs.

I have purposely generated errors in this page so that you may see how the program operates. You will see a bomb (or core dump) in the upper right corner of the screen. Clicking on that will launch the popup window, at which point you will be at error #1 with next and previous buttons to jump through the errors.

Using the Error Console

  • require_once "Error.php";
  • $reporter =& new ErrorReporter();
  • $reporter->setDateFormat('[Y-m-d H:i:s]');
  • $reporter->setStrictContext(false);
  • $reporter->setExcludeObjects(true);
  • $reporter->addReporter('console', E_VERY_ALL);
  • ErrorList::singleton($reporter, 'error');

Things to Know

  • You can specify to exclude dumping of objects
  • You can exclude certain class objects
  • You can specify number of source code lines
  • You can specify strict context for variables
  • $error->debug($myvar, 'myvar', __LINE__, __FILE__);
  • Click on bomb if error occurs in page (hidden if no errors)

About the Project

  • Author: Dan Allen
  • Version: Revision 1.0.2
  • Updated: 2003/02/04
  • License: LGPL
  • Project Page: mojavelinux.com