Login   Register  
PHP Classes
elePHPant
Icontem

File: error_handler.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Manuel Lemos  >  Log watcher  >  error_handler.php  >  Download  
File: error_handler.php
Role: Auxiliary script
Content type: text/plain
Description: Example of an error handler function that logs the error message, the file name, line number, function name, class name, including backtrace of all the function calls that lead to the line where the error occurred.
Class: Log watcher
Watch a log file and e-mail newly added lines
Author: By
Last change:
Date: 2003-11-30 06:18
Size: 1,402 bytes
 

Contents

Class file image Download
<?php

Function error_handler($error,$message,$file,$line)
{
    if(
$error error_reporting())
    {
        
$log=array();
        switch(
$error)
        {
            case 
E_ERROR:
                
$type='FATAL';
                break;
            case 
E_WARNING:
                
$type='ERROR';
                break;
            case 
E_NOTICE:
                
$type='WARNING';
                break;
            default:
                
$type='Unknown error type ['.$error.']';
                break;
        }
        
$log[]=$type.': '.$message.' in line '.$line.' of file '.$file.', PHP '.PHP_VERSION.' ('.PHP_OS.')';
        if(
function_exists('debug_backtrace'))
        {
            
$backtrace=debug_backtrace();
            for(
$level=1;$level<count($backtrace);$level++)
            {
                
$message='File: '.$backtrace[$level]['file'].' Line: '.$backtrace[$level]['line'].' Function: ';
                if(IsSet(
$backtrace[$level]['class']))
                    
$message.='(class '.$backtrace[$level]['class'].') ';
                if(IsSet(
$backtrace[$level]['type']))
                    
$message.=$backtrace[$level]['type'].' ';
                
$message.=$backtrace[$level]['function'].'(';
                if(IsSet(
$backtrace[$level]['args']))
                {
                    for(
$argument=0;$argument<count($backtrace[$level]['args']);$argument++)
                    {
                        if(
$argument>0)
                            
$message.=', ';
                        
$message.=serialize($backtrace[$level]['args'][$argument]);
                    }
                }
                
$message.=')';
                
$log[]=$message;
            }
        }
        
error_log(implode("\n\t",$log));
    }
    if(
$type==E_ERROR)
        exit(
1);
}

set_error_handler('error_handler');

?>