4

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

1
  • 1
    Have you tried set_error_handler? Commented Nov 29, 2011 at 9:37

3 Answers 3

9

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Would it be possible to get also the error type? (ie. notice, warning). And also, if my error handling function is part of a class, is there any way I can use it without making the method static?
@EduardLuca Yes, your custom error handler can be any "callback" so you could use an object and a method on it: uk.php.net/manual/en/…
@EduardLuca the $errno variable contains the type of the error (for example E_USER_ERROR or E_USER_WARNING). You can find the list of possible values here: php.net/manual/en/errorfunc.constants.php
3

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.

Comments

1

What about mixed set_error_handler ( callback $error_handler [, int $error_types = E_ALL | E_STRICT ] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.