如何在 PHP 中跟踪代码执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14341757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trace code execution in PHP?
提问by Martin Vseticka
I would like to see a log of THE WHOLE code execution of PHP script(s). Something like this: http://en.wikibooks.org/wiki/Ruby_Programming/Standard_Library/Tracer(for lack of better example; no flame please).
我想查看 PHP 脚本的整个代码执行日志。像这样的东西:http: //en.wikibooks.org/wiki/Ruby_Programming/Standard_Library/Tracer(因为没有更好的例子;请不要着火)。
Is there some way how to obtain the log in PHP?
有什么方法可以在PHP中获取日志?
Note:I know I can use a debugger but that's not the same.
注意:我知道我可以使用调试器,但这不一样。
回答by BenLanc
Xdebug is definitely what you want, but with something like callgrind from the valgrind suiteas well.
Xdebug 绝对是你想要的,但也有来自 valgrind 套件的 callgrind 之类的东西。
Zend blog post here should give you some pointers: http://devzone.zend.com/1139/profiling-php-applications-with-xdebug/
Zend 博客文章应该给你一些提示:http: //devzone.zend.com/1139/profiling-php-applications-with-xdebug/
回答by Naftali aka Neal
In any function you can see the whole backtrace by using debug_backtrace(...)
在任何函数中,您都可以通过使用查看整个回溯 debug_backtrace(...)
Or you can use Xdebug profilerto profile your PHP scripts.
或者您可以使用Xdebug 分析器来分析您的 PHP 脚本。
回答by UXE
回答by MVN
You can use a PHP extension called : XHProf, developed by Facebook.
您可以使用由 Facebook 开发的名为 XHProf 的 PHP 扩展。
It is capable of reporting function-level call counts and inclusive and exclusive wall time, CPU time and memory usage.
它能够报告函数级调用计数以及包含和专有的挂墙时间、CPU 时间和内存使用情况。
回答by Fil
This is what I need to traces all the lines when running php or Laravel framework in windows sub-system for linux (WSL).
这是我在 windows 子系统 linux (WSL) 中运行 php 或 Laravel 框架时需要跟踪所有行的内容。
Install xdebugin the system then edit /etc/php/7.4/mods-available/xdebug.iniwith the following details
xdebug在系统中安装,然后/etc/php/7.4/mods-available/xdebug.ini使用以下详细信息进行编辑
zend_extension=xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port = 9001
xdebug.scream=0
xdebug.cli_color=1
xdebug.show_local_vars=1
xdebug.remote_autostart=1
; this part here, above is used for line by line debug in vscode
xdebug.auto_trace=1
xdebug.trace_output_dir = /mnt/c/projects/www/phptraces
xdebug.trace_output_name=trace.%u
xdebug.collect_params=4
xdebug.trace_format = 1
This one here
这里的这个
xdebug.trace_output_dir = /mnt/c/projects/www/phptraces
is the path where to store logs
是存储日志的路径
For laravel framework it generate very large file most likely 4GB in size. So I used
对于 laravel 框架,它生成非常大的文件,最有可能是 4GB。所以我用
split -l 1000 trace.1576842503_368392.xt pieces/traces
to split it into smaller parts containing 1000 lines each and store it into pieces directory.
将其拆分为每个包含 1000 行的较小部分,并将其存储到pieces 目录中。
Then you can use editor find in files what your looking for
然后您可以使用编辑器在文件中查找您要查找的内容


