Reputation: 1915
How can I log the following error to a text file or database?
Fatal error: Call to undefined method PROJECTS::ssss()
Upvotes: 10
Views: 17622
Reputation:
Now in PHP 7 it is possible to catch fatal errors:
try {
ggggg(); // <---- make a fatal error
} catch(Throwable $e) {
var_dump($e);
}
Upvotes: 8
Reputation: 526
There is a way to cope with your task and actually you can set a custom error handler 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);
}
Upvotes: 13
Reputation: 318688
It is not possible to handle fatal errors using a custom error handler.
The best solution is simply enabling error logging (e.g. to syslog) in your php.ini and then using a tool like logcheck/logsentry to receive regular emails about unusual syslog entries.
Instead of syslog PHP can also log errors to a file - simply have a look at the error logging options of php.ini.
log_errors = On
error_log = syslog
error_log = /path/to/some/folder/phperrors.log
Obviously you only want to use one of the error_log
lines.
Upvotes: 12
Reputation: 34652
You could have all your base classes belong to a super-class 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 methods of classes derived from Base
would be ultimately handled by Base::__call()
. For static methods, accordingly, there's __callStatic()
(as of PHP 5.3).
Upvotes: 1
Reputation: 187
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
}
Upvotes: 0