PHP - Log stacktrace for warnings?
Asked Answered
D

4

26

Is it possible to log stacktraces for php warnings? Or catch a warning and error_log() it?

There's some code causing warnings in my error log, but it's impossible to know what's causing these warnings without knowing the stack trace.

Dyanna answered 21/6, 2011 at 14:15 Comment(0)
S
15

There is an example of using set_error_handler() in conjunction with ErrorException to do just this:

https://php.net/manual/en/class.errorexception.php

You would just need to implement your custom logging functionality inside of the handler function.


UPDATE:

Note, this also works for warnings, and many other error types. For full compatibility, see the manual for set_error_handler():

https://php.net/set_error_handler

Sweptback answered 21/6, 2011 at 14:19 Comment(2)
Does this work for warnings? It's not an error. Just a warning. The script continues.Dyanna
This technique will always leave the error handler as the first stack frame, right?Urochrome
A
21

Just throw this at the start of your script:

set_error_handler(function($severity, $message, $file, $line) {
    if (error_reporting() & $severity) {
        throw new ErrorException($message, 0, $severity, $file, $line);
    }
});

Remove the if you want to log everything, even if it's suppressed.

Abutilon answered 14/6, 2016 at 21:16 Comment(1)
Thanks dude, you saved ma a couple minutes of debug time and probably more in the future.Elva
S
15

There is an example of using set_error_handler() in conjunction with ErrorException to do just this:

https://php.net/manual/en/class.errorexception.php

You would just need to implement your custom logging functionality inside of the handler function.


UPDATE:

Note, this also works for warnings, and many other error types. For full compatibility, see the manual for set_error_handler():

https://php.net/set_error_handler

Sweptback answered 21/6, 2011 at 14:19 Comment(2)
Does this work for warnings? It's not an error. Just a warning. The script continues.Dyanna
This technique will always leave the error handler as the first stack frame, right?Urochrome
S
2

I believe xdebug would go to log if that's how you have it enabled in your php.ini file, but it has stack tracing (with some bonus features like showing the local variables). It is not recommend for a production environment though.

XDebug Stack Traces

Supersede answered 21/6, 2011 at 14:31 Comment(1)
Agree it should not be enabled long-term in a production environment, as it will slow down the server. But it is easier to enable than adding an error handler to your bootstrap initialization (which may be buggy in itself - why reinvent the wheel?), and will also diagnose errors arising from hackers accessing your PHP files directly. If you don't have a common bootstrap, or it's not used in all cases, it also much more convenient to catch all possible errors this way. Enable it for a day or two, then look at the logs to see if there are any genuine errors.Alkanet
S
2

Here you go:

class WarningWithStacktrace extends ErrorException {}
set_error_handler(function($severity, $message, $file, $line) {
    if ((error_reporting() & $severity)) {
        if ($severity & (E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE)) {
            $ex = new WarningWithStacktrace($message, 0, $severity, $file, $line);
            echo "\n" . $ex . "\n\n";
            return true;
        } else {
            throw new ErrorException($message, 0, $severity, $file, $line);
        }
    }
});

Errors/exceptions are thrown as usual, notices/warnings are printed only.

PS: Strict and other warning levels are not addressed, modify the code if needed...

Smokeless answered 9/12, 2019 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.