5

再会。

我一直在研究包含许多变量和会话的项目,并且大部分工作都是在“幕后”和通过 ajax 完成的。

问题是我尝试调试我的项目,但我找不到任何方法来跟踪和记录对某个变量所做的更改。

我一直在尝试使用 firephp 和 xdebug,但它们不显示何时更改了变量,只显示了它的最终值。

有什么解决办法吗?

4

2 回答 2

2

XDebug 可以跟踪变量更改,只需启用xdebug.collect_assignmentsxdebug.collect_params,因此当您生成跟踪日志文件时,您应该会看到更改。

示例配置:

xdebug.default_enable = 1           ; bool: The stacktraces will be shown by default on an error event.
xdebug.collect_vars = 1             ; bool: Gather information about which variables are used in a certain scope.
xdebug.show_local_vars=1            ; int: Generate stack dumps in error situations.
xdebug.collect_assignments=1        ; bool: Controls whether Xdebug should add variable assignments to function traces.
xdebug.collect_params=4             ; int1-4: Collect the parameters passed to functions when a function call is recorded.
xdebug.collect_return=1             ; bool: Write the return value of function calls to the trace files.
xdebug.var_display_max_children=256 ; int: Amount of array children and object's properties are shown.
xdebug.var_display_max_data=1024    ; int: Max string length that is shown when variables are displayed.
xdebug.var_display_max_depth=5      ; int: How many nested levels of array/object elements are displayed.
xdebug.trace_output_dir="/var/log/xdebug" ; string: Directory where the tracing files will be written to.

然后在您第一次分配变量之前或之后,通过将其添加到您的 PHP 代码中来开始跟踪代码:

xdebug_start_trace();

然后可以选择xdebug_stop_trace();在您认为已经发生的地方添加。

然后检查配置目录中生成的文件(由 指定xdebug.trace_output_dir)。如果文件很大,请使用特定变量过滤它grep,例如

grep --color=auto variable trace-log.xt

或通过以下方式将其过滤到较小的文件中:grep pattern trace-log.xt > smaller.log.txt.


另一种选择是使用phpdbg.

于 2016-07-08T12:38:38.780 回答
1

May be logged decorator can help you?

If you want to track some instance variables you can wrap them around with decorator that implements same interface. And into decorator methods you can write debug level log and then deligate workflow to original variables saved as decorator object field.

于 2013-06-08T19:41:35.880 回答