PHP: How to register fatal errors?

How can I write this error to a text file or database?

Fatal error: Call to undefined method PROJECTS::ssss() 
+7
source share
5 answers

it is impossible to handle fatal errors using a special error handler.

The best solution is to simply enable error logging (e.g. syslog) in your php.ini, and then use a tool like logcheck / logsentry to get regular messages about unusual syslog entries.
Instead of syslog, PHP can also write errors to a file - just look at the error logging options in php.ini.

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

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

+9
source

There is a way to cope with your task, and in fact you can install your own error handler for FATAL errors.

You can do it as follows:

 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); } 
+8
source

Now in PHP 7 you can catch fatal errors:

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


old post was catch(Error $e) changed to catch(\Throwable $e) although both work

+3
source

You can use all base classes for a superclass using the overload method :

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

Attempts to reference non-existent class methods derived from Base will ultimately be handled using Base::__call() . For static methods, respectively, there is __callStatic() (as from PHP 5.3).

+1
source

Sort of:

 if(!method_exists($obj, 'method')){ $db->log('What you want to log'); //log in your DB error_log('message');//Write to php error log } 
0
source

Source: https://habr.com/ru/post/908717/


All Articles