Last active
August 29, 2015 14:25
-
-
Save lsloan/073e212b60aa4ff13b01 to your computer and use it in GitHub Desktop.
A one-line solution to send an edited stack trace to the error log.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Print stack trace, removing repeated absolute path on each file | |
error_log(str_replace($_SERVER['DOCUMENT_ROOT'], '', (new Exception())->getTraceAsString())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP offers a couple of different ways to get a stack backtrace, but each has some drawbacks:
The alternative shown here instantiates an exception just to get the stack trace as a string. That's all that's needed. The code goes on to shorten each line by removing the repeated absolute paths to each file. (This assumes most of the files are within the document root that contains the application.) Finally, it's sent to the error log rather than cluttering up the output.