Recent Posts

PHP Error Reporting and Error Handling

PHP provides some Error Handling Functions to help us to keep track of problems issued in our applications. On the development environment, we can use some simple output functions to debug programs, but cannot do these on the production environment. Many developers will use log system to track problems(debugging).

System Logger

syslog() generates a log message that will be distributed by the system logger. On Windows NT, the syslog service is emulated using the Event Log. There are many types of operating system programs will be logged in system logger, such as kernel messages, system daemons and so forth. Since each process, application and operating system was written independently, there is little uniformity to the content of syslog messages. So, we should not use system logger in our applications.

//in php.ini
error_log = syslog
<?php
$current_time = date("Y-m-d H:i:s");
syslog(LOG_ERR, "Cann't connect to Memcached: 192.168.1.2 on {$current_time}");
?>

Error Handler

build-in error handler

If syntax errors or runtime errors occur when php scripts running, the error messages will be logged in specified files after you turn on log_errors and setting appropriate error_reporting.

//in php.ini
display_errors = Off
log_errors = On
<?php
error_reporting(E_ALL);
echo $valinv_www; //message: PHP Notice:  Undefined variable: valinv_www
error_reporting(0); //turn off
echo $valinv_try; //no message logged
?>

The messages logged in specified log files when you set error_log = php_errors.log in php.ini, otherwise will be logged in files which configured in web servers configuration:

Apache: ErrorLog /var/logs/apache_www.valinv.com.error_log;
Nginx: error_log  /var/logs/nginx_www.valinv.com.error_log;

user-defined error handler

<?php
function fsErrorHandler($errno , $errstr, $errfile, $errline, $errcontext)
{
	$error_message = "";
	switch ($errno) 
	{
	    case E_USER_ERROR:
	        $error_message .= "ERROR: [$errno] $errstr Fatal error on line $errline in file $errfile";
	        break;
	    case E_USER_WARNING:
	        $error_message .= "WARNING: [$errno] $errstr";
	        break;
	    case E_USER_NOTICE:
	        $error_message .= "NOTICE: [$errno] $errstr";
	        break;
	    default:
	        $error_message .= "Unknown error type: [$errno] $errstr";
	        break;
    }
    file_put_contents("./vv_user_defined.log", $error_message);
    /*
     * PHP build-in error handler is completely bypassed for the error types specified by error_types unless the callback function returns false
     * So return true to unuse PHP build-in error handler
     */
    return true;
}
//error_reporting() settings will have no effect and your error handler will be called regardless
error_reporting(0);
set_error_handler("vvErrorHandler");
echo $valinv_www; 
restore_error_handler(); //Restores the previous error handler function(Return to build-in error handler)
?>

trigger error

<?php
/*
 * trigger a user error condition, it can be used in conjunction with the build-in error handler (A), or with a user-defined error handler (B)
 *
 * A: error_reporting($level) which $level isn't 0, and ((haven't setted set_error_handler) or (have setted set_error_handler but after restored))
 * B: have setted set_error_handler but not restored (current error handler is user-defined)
 */
trigger_error("Con't connect to MySQL Server: 192.168.1.3", E_USER_ERROR);
?>

Other Debugging Functions

<?php
//....
print_r(debug_backtrace());
debug_print_backtrace();
error_log("Con't connect to MySQL Server: 192.168.1.3", 3, "./vv_error_log.log");
//....
?>

 

Share this Post