iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

August 7, 2008
Wish XML a happy birthday
About
 
August 7, 2008
Poll: How important is SEO to your overal website strategy?
About
 
August 7, 2008
How to Create a Search Feature with PHP and MySQL
WebReference.com
 
August 7, 2008
1 comment
.net
 
August 6, 2008
10 New SEO Reports
About
 
August 5, 2008
Array Creator
EarthWeb.com
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Create Own PHP Error Log File Archive 

  Views:    11635
  Votes:    6
by Chief Programmabilities 3/07/05 Rating: 

Synopsis:

Create your own error logs for debugging PHP scripts and save them in a log file for archival.
Pages: 
The Article

Since most hosters don't provide access to Apache error logs on shared hosting packages for technical reasons, you can create your own error logs for debugging PHP Scripts. (See the demo at http://programmabilities.com/error_log.php.)

Insert the following code in your PHP script. (Or create separate file and add the code in it. Include the file using include().)

/* we will do our own error handling. */
error_reporting(0); // Turns off all error reporting.

/* user defined error handling function. */
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars) 
{
    // timestamp for the error entry.
    $dt = date('Y-m-d H:i:s (T)');

    // define an assoc array of error string
    // in reality the only entries we should
    // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
    // E_USER_WARNING and E_USER_NOTICE.
    $errortype = array (
                E_ERROR => 'Error',
                E_WARNING => 'Warning',
                E_PARSE => 'Parsing Error',
                E_NOTICE => 'Notice',
                E_CORE_ERROR => 'Core Error',
                E_CORE_WARNING => 'Core Warning',
                E_COMPILE_ERROR => 'Compile Error',
                E_COMPILE_WARNING => 'Compile Warning',
                E_USER_ERROR => 'User Error',
                E_USER_WARNING => 'User Warning',
                E_USER_NOTICE => 'User Notice',
                E_STRICT => 'Runtime Notice'
                );
    // set of errors for which a var trace will be saved.
    $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  
    $err = "<errorentry>\n";
    $err .= "\t<datetime>" .$dt. "</datetime>\n";
    $err .= "\t<errornum>" .$errno. "</errornum>\n";
    $err .= "\t<errortype>" .$errortype[$errno]. "</errortype>\n";
    $err .= "\t<errormsg>" .$errmsg. "</errormsg>\n";
    $err .= "\t<scriptname>" .$filename. "</scriptname>\n";
    $err .= "\t<scriptlinenum>" .$linenum. "</scriptlinenum>\n";

    if (in_array($errno, $user_errors)) {
        $err .= "\t<vartrace>" .wddx_serialize_value($vars, 'Variables'). "</vartrace>\n";
    }
    $err .= "</errorentry>\n\n";

    // save to the error log file, and e-mail me if there is a critical user error.
    error_log($err, 3, '../error_log.log');
    if ($errno == E_USER_ERROR) {
        mail('bgates@gmail.com', 'Critical User Error', $err);
    }
}
$old_error_handler = set_error_handler('userErrorHandler');


PHP's error_reporting() function sets which PHP errors are reported. The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. The script's error_reporting(0) turns off all error reporting so the script can catch and save the errors to a file.

PHP's error_log() function sends an error message somewhere. This script uses it to send the error message to a file (option 3).

Now you have your own error log archives file that you can link to and look at! (See the demo at http://programmabilities.com/error_log.php.)



Chief Programmabilities
Programmabilities.com
Pages: 

Similar/related articles:


 
  Sponsors