Jump to content

What to Log with Errors


doubledee

Recommended Posts

My website is set up so that if there is an error somewhere, I assign an Error Code and redirect the User to a "Results Page" like this...

}else{
	// Update Failed.
	$_SESSION['resultsCode'] = 'EMAIL_NEW_EMAIL_FAILED_2129';
}//End of UPDATE EMAIL ADDRESS

// Close prepared statement.
mysqli_stmt_close($stmt5);

// Redirect to Display Outcome.
header("Location: " . BASE_URL . "/members/results.php");

// End script.
exit();

 

 

On the Results Page I display the error in User-friendly wording and usually a corrective action (e.g. "Return to Change E-mail")...

// Email Not Changed.
case 'EMAIL_NEW_EMAIL_FAILED_2129':
	echo '<h1>E-mail Change Failed</h1>';
	echo '<p>Your e-mail could not be changed due to a System Error.</p>';
	echo '<p>Please try again. (2129)</p>';
	echo '<a class="button" href="' . BASE_URL . '/members/change_email.php">Change E-mail</a>';
	break;

 

 

When my script is redirected to the Results Page, I would like to log the Error in my database.

 

What kinds of things should I log, and how can I take this not-so-sophisticated error handling code and make it better WITHOUT completely rewriting things?!

 

Thanks,

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/259132-what-to-log-with-errors/
Share on other sites

for one you could use the heredoc syntax instead of using echo abunch of times like this.

 



case 'EMAIL_NEW_EMAIL_FAILED_2129':


$error= <<<EOL
<h1>E-mail Change Failed</h1>
<p>Your e-mail could not be changed due to a System Error.</p>
<p>Please try again. (2129)</p>
<a class="button" href="' . BASE_URL . '/members/change_email.php">Change E-mail</a>
EOL;

echo $error;

break;

 

 

What you log will depend on what the nature of the error is.  For instance, for a DB error you would want to log the query text you are using and any variables your using as parameters for that query.  Then you'll be able to re-assemble the query and see what went wrong.

 

If you had an error say opening a file you'd want to log the name of the file being opened, what the error was (permission denied, non-existent, etc), and for good measure maybe the [m=getcwd]current working directory[/m] and [m=get_include_path]include_path setting[/m].

 

There could be various other types of errors you might have, each will have it's own data that needs logged.

 

Since the information you need to log can vary from error to error you'll need to gather all that information at the time of the error and either pass it to your result script or log the error at that point and just have the result script display the message to the user.

 

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.