How to Log or mail Errors in PHP Code
There are a number of things that can go wrong in code and they bound to through errors, even if you’ve tested them hundreds of times. PHP provides some error handling methods such as error_reporting(), set_error_handler(), error_log(), restore_error_handler() for handling errors in the PHP code.
error_reporting – Sets which PHP errors are reported.
int error_reporting ([ int $level] )
error_reporting() level constants
<?php error_reportinng(E_ALL);?>
error_log – Sends an error message to the web server’s error log, a TCP port or to a file.
bool error_log ( string $message [, int $message_type [, string $destination [, string $extra_headers ]]] )
Parameters
$message – The error message that should be logged.
$message_type – Says where the error should go. The possible message types are as follows:
$extra_headers – The extra headers. It’s used when the message_type parameter is set to 1. This message type uses the same internal function as mail() does.
restore_error_handler – Restores the previous error handler function
bool restore_error_handler ( void )
<?phprestore_error_handler();?>
set_error_handler – Sets a user-defined error handler function
mixed set_error_handler ( callback $error_handler [, int $error_types] )
Example:
<?php
error_reporting(E_ALL);
function on_error($num, $str, $file, $line) {
print "Encountered error $num in $file, line $line: $str\n";
error_log("Error: Encountered error $num in $file, line $line: $st",1, "dev@gmail.com","From: sys@gmail.com");
}
set_error_handler("on_error");
restore_error_handler();?>
Posted by Shahid