Page 1 of 1

Program to find PHP Errors

Posted: Mon Mar 30, 2009 2:28 pm
by clewis
I am looking for a program that will specifically let you see which PHP scripts are generating errors and where.

Most of them show you “errors” but they are 401,403,404,500, etc. I want a program that will do more.

We’re logging all our php errors to a log file. We need a program that could analyze this file. I'd like the report
to contain the most common errors and the number of times they occurred during the report period.

Do any of you have solutions that you have used that you would recommend?

Thank you.

Re: Program to find PHP Errors

Posted: Tue Mar 31, 2009 2:07 am
by jaoudestudios
Do you use SVN subversion? There is a program that checks the code everytime it is committed to the repository and can even run your test layer too. So if anyone breaks the trunk they get named and shamed :lol:

Re: Program to find PHP Errors

Posted: Tue Mar 31, 2009 2:37 am
by Mark Baker
execute a command line php -l against every script whenever it's been modified


Don't just log errors, generate a stack trace: a lot more useful for debugging

Code: Select all

 
error_reporting(0);
 
 
function dumpStackTraceEntry($stacktraceCount,$stacktrace)
{
    $output = $args = null;
    if ((isset($stacktrace['args'])) && (is_array($stacktrace['args']))) {
        foreach ($stacktrace['args'] as $a) {
            if (!is_null($args)) { $args .= ', '; }
            switch (gettype($a)) {
                case 'integer':
                case 'double':      $args .= $a;
                                    break;
                case 'array':       $args .= 'Array('.count($a).')';
                                    break;
                case 'object':      $args .= 'Object('.get_class($a).')';
                                    break;
                case 'resource':    $args .= 'Resource('.strstr($a, '#').')';
                                    break;
                case 'boolean':     $args .= $a ? 'True' : 'False';
                                    break;
                case 'NULL':        $args .= 'Null';
                                    break;
                case 'string':      $a = htmlspecialchars(substr($a, 0, 128)).((strlen($a) > 128) ? '...' : '');
                                    $args .= "\"$a\"";
                                    break;
                default:            $args .= 'Unknown';
            }
        }
    }
 
    $output .= "\t\t<stackTraceEntry>\n";
    $output .= "\t\t\t<stackTraceLevel>$stacktraceCount</stackTraceLevel>\n";
    $output .= "\t\t\t<stackTraceFile>{$stacktrace['file']}</stackTraceFile>\n";
    $output .= "\t\t\t<stackTraceLine>{$stacktrace['line']}</stackTraceLine>\n";
    $output .= "\t\t\t<stackTraceCall>";
    if ((isset($stacktrace['class'])) && (isset($stacktrace['type']))) {
        $output .= $stacktrace['class'].$stacktrace['type'];
    }
    $output .= "{$stacktrace['function']}(".$args.")</stackTraceCall>\n";
    $output .= "\t\t</stackTraceEntry>\n";
 
    return $output;
}   //  function dumpStackTraceEntry()
 
 
function generateErrorStackTrace()
{
    $output = '';
 
    if (function_exists('debug_backtrace')) {
        $stacktraceDetails = debug_backtrace();
        $stacktraceCount = count($stacktraceDetails);
 
        foreach ($stacktraceDetails as $stacktrace) {
            if (($stacktrace['function'] <> 'generateErrorStackTrace') && ($stacktrace['function'] <> 'userErrorHandler')) {
                $output .= dumpStackTraceEntry($stacktraceCount,$stacktrace);
            }
            $stacktraceCount--;
        }
        if ($output > '') { $output = "\t<stackTrace>\n".$output."\t</stackTrace>\n"; }
    }
 
    return $output;
}   //  function generateErrorStackTrace()
 
 
function generateExceptionStackTrace($exception)
{
    $output = '';
 
    $stacktraceDetails = $exception->getTrace();
    $stacktraceCount = count($stacktraceDetails);
 
    foreach ($stacktraceDetails as $stacktrace) {
        $output .= dumpStackTraceEntry($stacktraceCount,$stacktrace);
        $stacktraceCount--;
    }
 
    if ($output > '') { $output = "\t<stackTrace>\n".$output."\t</stackTrace>\n"; }
 
    return $output;
}   //  function generateExceptionStackTrace()
 
 
function ignoreError($errorNumber)
{
    global $ignoreErrorCodes;
 
    if (in_array($errorNumber,$ignoreErrorCodes)) {
        return true;
    } else {
        return false;
    }
}   //  function ignoreError()
 
 
function generateVarTrace($errorNumber)
{
    global $varTraceErrors;
 
    if (in_array($errorNumber,$varTraceErrors)) {
        return true;
    } else {
        return false;
    }
}   //  function generateVarTrace()
 
 
// user defined error handling function
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
    global $PHPErrorLogFile;
 
    //  timestamp for the error entry
    $dt = date("Y-m-d H:i:s (T)");
 
    $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 is only valid in PHP 5
        E_STRICT            => "Runtime Notice"
    );
 
    if (!ignoreError($errno)) {
        $err = "<errorEntry>\n";
        $err .= "\t<dateTime>{$dt}</dateTime>\n";
        $err .= "\t<errorNumber>{$errno}</errorNumber>\n";
        if (isset($errortype[$errno])) { $err .= "\t<errorType>{$errortype[$errno]}</errorType>\n"; }
        $err .= "\t<errorMessage>{$errmsg}</errorMessage>\n";
        $err .= "\t<scriptName>{$filename}</scriptName>\n";
        $err .= "\t<scriptLineNumber>{$linenum}</scriptLineNumber>\n";
        if (generateVarTrace($errno)) {
            //  Do a dump of variables for any user-level errors
            $err .= "\t<vartrace>".wddx_serialize_value($vars, "Variables")."</vartrace>\n";
        }
        //  dump a stack trace
        $err .= generateErrorStackTrace();
 
        $err .= "</errorEntry>\n\n";
 
        //  save to the error log
        error_log($err, 3, $PHPErrorLogFile);
    }
}   //  function userErrorHandler()
 
 
// user defined exception handling function
function userExceptionHandler($exception)
{
    global $PHPErrorLogFile;
 
    //  timestamp for the error entry
    $dt = date("Y-m-d H:i:s (T)");
 
    $err = "<errorEntry>\n";
    $err .= "\t<dateTime>{$dt}</dateTime>\n";
    $err .= "\t<errorNumber>".$exception->getCode()."</errorNumber>\n";
    $err .= "\t<errorType>Thrown Exception</errorType>\n";
    $err .= "\t<errorMessage>".$exception->getMessage()."</errorMessage>\n";
    $err .= "\t<scriptName>".$exception->getFile()."</scriptName>\n";
    $err .= "\t<scriptLineNumber>".$exception->getLine()."</scriptLineNumber>\n";
    //  dump a stack trace
    $err .= generateExceptionStackTrace($exception);
    $err .= "</errorEntry>\n\n";
 
    //  save to the error log
    error_log($err, 3, $PHPErrorLogFile);
}   //  function userExceptionHandler()
 
$PHPErrorLogFile = 'errorLog.txt'
$old_error_handler = set_error_handler("userErrorHandler");
$old_exception_handler = set_exception_handler('userExceptionHandler');