Often you get a fatal error to deal with but where the problem code is not always very clear where the problem is.
This is especially difficult for fatal errors like "NOTICE: PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
.
When memory errors any debugging can easily influence the location the fatal is triggered.
Everything on the Internet states that you cant get a stack trace for a fatal. The most common suggested solution is to use a custom shutdown function to at least handle the fatal in some way maybe log it.
The following is a workaround that uses a debug function for storing a trace every time it is called and then in the shutdown function we retrieve the last trace that was stored and write it all to a fatal specific error log.
The russian-roulette.php
file is an example demo that every time you run it will put a bullet in the gun class spin it and pull the trigger and if the shot returned is a bullet trigger a fatal.
Generating logs as per the below.
2018-07-05 11:46:38: Fatal Error: Oops, sorry you dead! in /var/www/public/russian-roulette.php on line 118
Last Debug Trace:
1) /var/www/public/russian-roulette.php(172): debug_track_trace()
2) /var/www/public/russian-roulette.php(117): Gun->pullTrigger()
3) /var/www/public/russian-roulette.php(183): spin_and_pull_trigger()
2018-07-05 12:12:37: Fatal Error: Oops, sorry you dead! in /var/www/public/russian-roulette.php on line 118
Last Debug Trace:
1) /var/www/public/russian-roulette.php(172): debug_track_trace()
2) /var/www/public/russian-roulette.php(117): Gun->pullTrigger()
3) /var/www/public/russian-roulette.php(183): spin_and_pull_trigger()
2018-07-05 12:12:40: Fatal Error: Oops, sorry you dead! in /var/www/public/russian-roulette.php on line 118
Last Debug Trace:
1) /var/www/public/russian-roulette.php(172): debug_track_trace()
2) /var/www/public/russian-roulette.php(117): Gun->pullTrigger()
3) /var/www/public/russian-roulette.php(183): spin_and_pull_trigger()
This does not log any trace unless you add debug_track_trace()
and rather than giving you detailed logging gives you a way of reproducing a fatal with some temporary debug code added