Login |  Register 
Your PHP skills tool showed me were I needed to improve and how to do it!
Tom
 

Logging

PHP comes with built in logging facilities. But why log problems? Well for anyone who has had to diagnose problems in a web application logging is essential, it allows you to cross reference when a problem occured with actually what happened in the code. Proper error handling and logging is therefore essential. One of the best ways to error log is to log errors when they are caught in a try catch block. We will discuss the easiest way to implement logging using the error_log function. The error_log function has the following format.
error_log($message, $message_type, $destination)
The desitination parameter is optional and depends on value of the message type parameter. The three types of message are: If message type 1 or 3 are chosen then the destination needs to be set, for message type 1 this will be a email address and for message type 3 it will be a path to a log file.
try
{
 /*
  * Error happens here
  */
}
catch (Exception $e)
{
 error_log("A problem occured, the error is: $e->getMessage(), 0);
}
Logging to the system log is better than nothing,. however you will have to find your applications messages from all the other messages inside the system log and there can be quite a lot. I usually prefer using a log dedicated just to my application.
try
{
 /*
  * Error happens here
  */
}
catch (Exception $e)
{
 error_log("A problem occured, the error is: $e->getMessage(), 3, "errors.log");
}
Finally for very serious errors you can use the email address, I tend to only do this for serious errors to help avoid a email box being flooded with more mundane problems, although ideally there should be no errors what so ever!
try
{
 /*
  * Error happens here
  */
}
catch (Exception $e)
{
 error_log("A problem occured, the error is: $e->getMessage(), 1, "errors@example.com");
}

No comments have been provided.
security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38