简体   繁体   中英

Custom PHP error handler

I have tried to find s better PHP error handler, one that will be easier to read in html, but I don't see any on google.

I tried to make my own, but the task of figuring out the commands was more than I had time for and I ran into many errors.

Here is the garbage that php is producing right now (files, classes and function names have been changed):

Fatal error: Cannot redeclare bbb() (previously declared in /home/user/path/1.php:5) in /home/user/path/1.php on line 26 Call Stack: 0.0309 662280 1. {main}() /home/user/path/1.php:0 0.0610 5610272 2. su::aaa() /home/user/path/1.php:21 0.0610 5610752 3. su::__callStatic() /home/user/path/class.su.php:0 0.0610 5611456 4. call_user_func_array() /home/user/path/class.su.php:27 0.0610 5611912 5. user->aaa() /home/user/path/class.su.php:0 0.0628 5670816 6. user->aaa() /home/user/path/class.user.php:3342 0.0628 5671640 7. user->aaa() /home/user/path/class.user.php:3407 0.0628 5672464 8. user->sss() /home/user/path/class.user.php:3449 0.0628 5679576 9. DA->ccc() /home/user/path/class.user.php:3475 

It is all one line and I can't parse it well in my head to see what is actually happening. I want though this call stack to be printed as nice html.

Thanks, Elijah

Use set_error_handler() It's designed specifically for that. Define a function that accepts the following arguments:

handler_function ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )

And have set_error_handler() to use that instead of the default PHP handler. Make sure to read the entire manual entry first!

There is a clean implementation of what you want http://il.php.net/manual/en/function.set-error-handler.php and can easily be customized

The Code

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if (! (error_reporting () & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    switch ($errno) {
        case E_USER_ERROR :
            echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
            echo "  Fatal error on line $errline in file $errfile";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
            echo "Aborting...<br />\n";
            exit ( 1 );
            break;

        case E_USER_WARNING :
            echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
            break;

        case E_USER_NOTICE :
            echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
            break;

        default :
            echo "Unknown error type: [$errno] $errstr<br />\n";
            break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

If this is not what you want then explain better

The function that I was looking for was debug_backtrace. By implementing this in the error handler, I was able to print the call stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM