[ACCEPTED]-How can I log fatal errors in PHP?-fatal-error

Accepted answer
Score: 12

There is a way to cope with your task and 2 actually you can set a custom error handler 1 on fatal errors.

You can do it this way:

ini_set('error_reporting', E_ERROR);
register_shutdown_function("fatal_handler");
function fatal_handler() {
    $error = error_get_last();
    // Do whatever you want with this error. For example:
    YourDBApplicationLayer::writeFatal($error);
}
Score: 11

It is not possible to handle fatal errors using a custom 9 error handler.

The best solution is simply 8 enabling error logging (e.g. to syslog) in 7 your php.ini and then using a tool like 6 logcheck/logsentry to receive regular emails 5 about unusual syslog entries.
Instead of 4 syslog PHP can also log errors to a file 3 - simply have a look at the error logging 2 options of php.ini.

log_errors = On
error_log = syslog
error_log = /path/to/some/folder/phperrors.log

Obviously you only want 1 to use one of the error_log lines.

Score: 7

Now in PHP 7 it is possible to catch fatal errors:

try {
    
    ggggg(); // <---- make a fatal error
    
} catch(Throwable $e) {
    
    var_dump($e);
    
}

0

Score: 1

You could have all your base classes belong to a super-class 4 utilizing method overloading:

class Base 
{
    public function __call($name)
    {
        MyLog::logError(...);
        trigger_error("Function ".get_class($this)."::$name doesn't exist", 
            E_USER_ERROR);
    }
}

Attempts to invoke non-existing 3 methods of classes derived from Base would be 2 ultimately handled by Base::__call(). For static methods, accordingly, there's 1 __callStatic() (as of PHP 5.3).

Score: 0

Something like:

if(!method_exists($obj, 'method')){

   $db->log('What you want to log'); //log in your DB
   error_log('message');//Write to php's error log

}

0

More Related questions