Zend - The PHP Company




Miscellaneous

Add Code


Enhanced error logging  

Type: code fragment
Added by: ddamian
Entered: 04/09/2002
Last modified: 09/12/2001
Rating: **** (3 votes)
Views: 4322
The most interesting feature is probably displaying the highlighted source code that triggered the errors. It also allows logging errors to syslog and dumping context on fatal errors.


/* $Id$ */

function custom_error_handler ($errno, $msg, $file, $line, $context) {
  
  switch ($errno) {
  case E_ERROR:
  case E_USER_ERROR:
    $type = "FATAL";
    __custom_error_log ($type, $msg, $file, $line);
    __verbose_error ($type, $errno, $msg, $file, $line, $context);
    if (defined ("DEBUG") && DEBUG)
      __show_error_source ($type, $msg, $file, $line, "#c00000");
    die ();
    break;
  case E_WARNING:
  case E_USER_WARNING:
    $type = "WARNING";
    __custom_error_log ($type, $msg, $file, $line);
    break;
  case E_NOTICE:
  case E_USER_NOTICE:
    $type = "NOTICE";
    break;
  default:
    $type = "ERROR ($errno)";
    __custom_error_log ($type, $msg, $file, $line);
    break;
  }
  if (defined ("DEBUG") && DEBUG)
    __show_error_source ($type, $msg, $file, $line);
}


/* private_functions */


function __custom_error_log ($type, $msg, $file, $line)
{
  global $HTTP_X_FORWARDED_FOR, $REMOTE_ADDR, $HTTP_USER_AGENT, $REQUEST_URI;
  
  $ip_addr = $REMOTE_ADDR;
  if ($HTTP_X_FORWARDED_FOR)
    $ip_addr .= " ($HTTP_X_FORWARDED_FOR)";
  
  if (defined ("APPNAME"))
    $name = APPNAME;
  else
    $name = "php";

  error_log ("[$name] $type: $msg in $file:$line
[$name-error-details] $ip_addr [$HTTP_USER_AGENT] URI: $REQUEST_URI", 0);
}


function __verbose_error ($type, $errno, $msg, $file, $line, $context) {
  global $SERVER_ADMIN;

  print "<h1>Error!</h1>";
  print "An error occurred while executing this script. Please
contact the <a href=mailto:$SERVER_ADMIN>$SERVER_ADMIN</a> to
report this error.";
  print "<p>";
  print "Here is the information provided by the script:";
  print "<hr><pre>";
  print "Error type: $type (code: $errno)<br>"; 
  print "Error message: $msg<br>"; 
  print "Script name and line number of error: $file:$line<br>"; 
  print "Variable context when error occurred: <br>";
  print_r ($context); 
  print "</pre><hr>";
}


function __show_error_source ($type, $msg, $file, $line)
{  
  global $__src_array;
  $line_offset = 3;

  if (! isset ($__src_array[$file]))
    $__src_array[$file] = @file ($file);
  
  if (!$__src_array[$file])
    return;
  
  if ($line - $line_offset < 1)
    $start = 1;
  else
    $start = $line - $line_offset;
  
  if ($line + $line_offset > count ($__src_array[$file]))
    $end = count ($__src_array[$file]);
  else
    $end = $line + $line_offset;
  
  print "<table cellpadding=1 cellspacing=0 border=0 bgcolor=#808080 width=80%><tr><td>";  
  print "<table cellpadding=2 cellspacing=0 border=0 bgcolor=white width=100%>";
  for ($i = $start; $i <= $end; $i++) {
    $str = @highlight_string ("<?" . $__src_array[$file][$i-1] . "?>", TRUE);

    $pos1 = strpos ($str,"&lt;?");
    $pos2 = strrpos ($str,"?&gt;");

    $str = substr ($str, 0, $pos1) .
      substr ($str, $pos1+5, $pos2-($pos1+5)) .
      substr ($str, $pos2+5);
    
    ($i == $line) ? $bgcolor = "bgcolor=#ffccaa" : $bgcolor = "";    
    print "<tr><td bgcolor=#d0d0d0 width=15 align=right><code>$i</code></td>
      <td $bgcolor>$str</td></tr>";
  }
  
  print "<tr bgcolor=#c00000>
    <td colspan=2 nowrap>
      <font color=#ffffaa><code> $type: $msg</code></font>
    </td></tr>
  </table></td></tr></table><code><font color=gray>File: $file</font></code><p>";
}

?>


Usage Example


<?php
define 
("DEBUG"TRUE);
include 
"error.inc";
set_error_handler ("custom_error_handler");
error_reporting (E_ALL);

join (""""); // should generate a warning
?>


Rate This Script





Search



This Category All Categories