How to set up error reporting with the Stanford Web Application Toolkit

From Web Services Wiki

Jump to: navigation, search

Contents

Problem

You want to change PHP's default error handling settings.

Solution

Use SWAT to customize PHP's error handling settings for your application.

List of modes

The toolkit grants the developer options for three different error reporting modes which should suit the needs of most applications. Please note that changing the error reporting mode in SWAT takes effect over the whole application, not just toolkit-specific functionality. All errors, whether they occur within the toolkit itself or not, will be reported the same way.

Development

Set the error reporting mode to development to show all errors except for notices in the browser. Recommended for the initial development of an application. May also use the constant StanfordUtil::DEVELOPMENT.

Production

Set the error reporting mode to production to write errors to the web server's error log and not display them in the browser. Recommended for live sites. May also use the constant StanfordUtil::PRODUCTION.

Debugging

Set the error reporting mode to debugging to display all errors and notices. Recommended for temporary debugging purposes during development. May also use the constant StanfordUtil::DEBUGGING.

Using a configuration file

Setting up a configuration file automates a lot of the toolkit's behavior. Read How to set up a configuration file for the Stanford Web Application Toolkit for more information.

You may change the option for logging mode under the settings subsection to modify PHP's error handling settings.

Configuration file (config.yaml):

settings:
 
  logging mode:  development

PHP script:

// Include StanfordApp
include_once("stanford.app.php");
 
// Create a new StanfordApp
$app = new StanfordApp("/path/to/config.yaml");
 
// From here on, the custom error reporting mode is in effect (no further action is required)

Manual configuration

Using StanfordApp

If you already have a StanfordApp object in your application, call the set_error_reporting function in the util module.

// Set error reporting mode to 'development'
$app->util->set_error_reporting(StanfordUtil::DEVELOPMENT);
 
// Custom error reporting mode is in effect

Standalone method

To use the function by itself, include StanfordUtil and call set_error_reporting directly.

// Include StanfordUtil
include_once("stanford.util.php");
 
// Set the error reporting level
StanfordUtil::set_error_reporting(StanfordUtil::DEVELOPMENT);

Discussion

What changes are made to the configuration when a particular mode is chosen?

For development:

// Report all PHP errors except for notices
ini_set('error_reporting', E_ALL ^ E_NOTICE);
 
// Set the display_errors directive to On
ini_set('display_errors', 1);
 
// Do not log errors to the web server's error log
ini_set('log_errors', 0);

For debugging:

// Report all PHP errors and notices
ini_set('error_reporting', E_ALL);
 
// Set the display_errors directive to On
ini_set('display_errors', 1);
 
// Do not log errors to the web server's error log
ini_set('log_errors', 0);

For production:

// Report simple running errors
ini_set('error_reporting', E_ALL ^ E_NOTICE);
 
// Set the display_errors directive to Off
ini_set('display_errors', 0);
 
// Log errors to the web server's error log
ini_set('log_errors', 1);
Personal tools