After upgrading my PHP version from 7.2 to 8.0.3, my custom error handler broke.
Here's the error:
[date/time] Trying to access array offset on value of type null | err_error_log.php | line:36 (warn)
The error occurs here: switch ($lasterror['type']){
Here's the full code:
<?php
set_error_handler("errorHandler");
register_shutdown_function("shutdownHandler");
function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context=""){
$error = $error_message." | ".$error_file." | line:".$error_line;
switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
mylog($error, "(fatal)");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
mylog($error, "(error)");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
mylog($error, "(warn)");
break;
case E_NOTICE:
case E_USER_NOTICE:
mylog($error, "(info)");
break;
case E_STRICT:
mylog($error, "(debug)");
break;
default:
mylog($error, "(warn default)");
}
}
function shutdownHandler(){ //will be called when php script ends.
$lasterror = error_get_last();
switch ($lasterror['type']){
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
case E_NOTICE:
case E_USER_NOTICE:
case E_STRICT:
case E_PARSE:
$error = $lasterror['message']." | ".$lasterror['file']." | line:".$lasterror['line'];
mylog($error, "fatal");
}
}
function mylog($error, $errlvl){
error_log($error." ".$errlvl."\n"."https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI] ",0);
$filename="tmp/variables.txt";
if (file_exists($filename)){
$bestand=fopen($filename, "r");
$variables=fread($filename, filesize($filename));
error_log("Variabels: ".$variables,0);
}
}
?>
Probably it's something stupid, but I can't seem to fix it... It has something to do with $lasterror['type'] being empty or something, if I understand it correctly from this topic: How to fix Trying to access array offset on value of type null error
Any help is appreciated! Thx in advance!
Fixed it like this:
function shutdownHandler(){ //will be called when php script ends.
$lasterror = error_get_last();
if ($lasterror!=null){
switch ($lasterror['type']){ ...
Thx @El_Vanja