William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

PHP: Project logs setting other than webserver’s

PHP: Project logs setting other than webserver’s

It is important to configure PHP’s error reporting settings depending on which phase of development. Generally, it is a good idea to see all warnings and errors in the web browser during the initial phase; and later, once the site has launched, send error messages to a log file so that visitors do not see them.

1. Error Reporting for Development

During development, you want to display all errors and warnings to the browser.

// Report all PHP errors
ini_set('error_reporting', E_ALL);
// Set the display_errors directive to On
ini_set('display_errors', 1);

2. Error Reporting for Production

In production, you may want to lower the error reporting level and not display errors to the browser.

// 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); 
ini_set('error_log', LOGDIR.'/error_log'); 

3. Logging errors

You can use the PHP function error_log() to send errors to your own log file or an e-mail address:

// Destinations
define("ADMIN_EMAIL", "nobody@site.com");
define("LOG_FILE", "/home/log/errors.log");
// Destination types
define("DEST_EMAIL", "1");
define("DEST_LOGFILE", "3");
/* Examples */
// Send an e-mail to the administrator
error_log("Fix me!", DEST_EMAIL, ADMIN_EMAIL);
// Write the error to our log file
error_log("Error", DEST_LOGFILE, LOG_FILE);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: