Jump to content

Custom Error Handler


devxtec

Recommended Posts

I have written my own function which will handle all error messages issues by my script. The current function output looks as follows.

 

Date: 05-09-2008 21:54:48

Error type: 8

Error message: Undefined index:  id

Script: (PATH)/main/index.php(18)

Host: domain.com

Client: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1

Client IP: XXX.XXX.XXX.XXX

Request URI: /main/

 

How can I get my function shown below to change the Error type to be (Critical, Fatal, Notice, etc)? Would I want to use a case statement?

 

Here is my function and if anyone knows of something more efficient that could be done in my function I'm all ears.

 

//	function to handle all error messages
//	If a critical error is encountered an SMS message is sent.
//	Accepted Input:   $type
//					  $msg
//					  $file
//					  $line
//	Returned Values:  none
// usage: set_error_handler("errorMsg"); at the top of the php file
function errorMsg($type, $msg, $file, $line)
{
$errorStr = "Date: " . date("d-m-Y H:i:s", mktime()) . "\n";
$errorStr .= "Error type: $type\n";
$errorStr .= "Error message: $msg\n";
$errorStr .= "Script: $file($line)\n";
$errorStr .= "Host: " . $_SERVER['HTTP_HOST'] . "\n";
$errorStr .= "Client: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
$errorStr .= "Client IP: " .$_SERVER['REMOTE_ADDR'] . "\n";
$errorStr .= "Request URI: " . $_SERVER['REQUEST_URI'] . "\n\n";

$filename = "dn_error.log";

// Look for the word critical in the $type variable. Returns False if not found.
$criticalError = stripos($type, "critical");
if(file_exists($filename)) {
	// Log the error to the file
	error_log($errorStr, 3, "$filename");

	// If a critical error is detected send notification to cell phone
	if($criticalError !== false) {
		if(!mail("[email protected]", "Critical Error Log Addition for domain.com", "A critical error has been appended to the error log.")) {
			echo "Critical error has been detected. We were unable to contact the webmaster. Please email [email protected]";
			break;
		}
	} else { // otherwise send an email to personal inbox for checking
		if(!mail("[email protected]", "Error Log Addition for domain.com", "An error has been appended to the error log.")) {
			echo "Critical error has been detected. We were unable to contact the webmaster. Please email [email protected]";
			break;
		}
	}
} else {
	// IF UNABLE TO LOG INFORMATION THEN OUTPUT ERRORS TO SCREEN AND INCLUDE INFO IN THE EMAIL
	echo "Unable to log error.";
	break;
}
// STILL TO IMPLEMENT:
// IF EMAIL FAILS HAVE MESSAGE STATE TO CONTACT A WEBMASTER WITH THE ERROR INFORMATION OUTPUTTED.
}

Link to comment
https://forums.phpfreaks.com/topic/122970-custom-error-handler/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.