I am trying to log the errors using register_shutdown_function and set_error_handler functions. I am using the following class file for the same. However it does not work.
<?php
//Logs.php
class MyLogs
{
public function __construct()
{
register_shutdown_function(array($this, 'shutdownHandler'));
set_error_handler(array($this, 'errorHandler'));
}
private function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context)
{
$error = "lvl: " . $error_level . " | msg:" . $error_message . " | file:" . $error_file . " | ln:" . $error_line;
switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
case E_USER_ERROR:
$this->logMe($error, "fatal");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$this->logMe($error, "error");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
$this->logMe($error, "warn");
break;
case E_NOTICE:
case E_USER_NOTICE:
$this->logMe($error, "info");
break;
case E_STRICT:
$this->logMe($error, "debug");
break;
default:
$this->logMe($error, "warn");
}
}
private function shutdownHandler() //will be called when php script ends.
{
$lasterror = error_get_last();
echo "Level: " . $lasterror;
switch ($lasterror['type'])
{
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_PARSE:
$error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line'];
$this->logMe($error, "fatal");
}
}
private function logMe($error, $errlvl)
{
echo 'Error No: ' . $error . ' <BR> Error Level: ' .$errlvl;
}
}
..
<?php
// Index.php
include "Logs.php"
new MyLogs();
echo $b; //this should generate a notice error since $b does not exits...
However, if I do not use a class but just functions, they work fine...can someone please tell me whats wrong?
Thanks
You should set properly your error settings. For example
ini_set('error_reporting', -1);
ini_set('display_errors', 1);
After this you would find your error: your shutdownHandler
and errorHandler
have to be public.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With