Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch all PHP errors in array

Is there any way I can get all notices, warnings, errors etc that PHP encounters stored in an array?

I need this for a custom error logger and I want it to catch also errors in addition to exceptions, which I already did.

I managed to find something for the last error, but that's not enough: error_get_last

like image 534
Eduard Luca Avatar asked Dec 12 '22 07:12

Eduard Luca


2 Answers

You will have to build a custom error handler: http://www.php.net/manual/en/function.set-error-handler.php

$_ERRORS = array();

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    global $_ERRORS;
    $_ERRORS[] = array("errno" => $errno, "errstr" => $errstr, "errfile" => $errfile, "errline" => $errline);
}

set_error_handler("myErrorHandler");
like image 124
Marijn van Vliet Avatar answered Dec 21 '22 07:12

Marijn van Vliet


You could set a custom error handler and then either use a static variable to collect all the errors or write them as they occur to some kind of persistent storage.

like image 33
liquorvicar Avatar answered Dec 21 '22 06:12

liquorvicar