zidizei/debug

PHP 的简单调试工具。

0.1.1 2015-02-14 13:15 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:23:04 UTC


README

PHP 的简单调试工具,可以输出消息到 stdout 或浏览器 JavaScript 控制台(如果支持)。

深受 debug(用于 node.js)的启发。

Latest Stable Version Total Downloads License

安装

使用 Composer 并将其添加到您的 composer.json

"require": {
    "zidizei/debug", "~0.1.1"
}

使用方法

debug-php 允许您将简短的调试消息打印到您的 PHP 项目的 stdout 或浏览器的 JavaScript 控制台(如果您在网站上使用此库)。

require "vendor/autoload.php";

// initiate debug profile named 'parser'
\Debug\profile("parser");

\Debug\debug("Parsing some stuff now");
/* Parse the stuff */
\Debug\debug("Stuff successfully parsed");

// close the current debug profile (in this case 'parser')
\Debug\profile();

当使用 Web 服务器和浏览器运行上述代码时,将产生类似以下内容:

parser debug

使用 shell 定义的颜色的输出到 stdout 将大致相同。

禁用调试消息

默认情况下,只需调用其方法即可使用 debug-php。从 0.1.1 版本开始,您可以通过调用 \Debug\off() 来明确禁用调试消息。与 \Debug\on() 结合使用,这可以用于程序化决定跳过代码中某些部分的调试消息。

多个配置文件

您可以使用 配置文件 来更好地区分一些调试消息。上面的例子只通过调用

\Debug\profile("parser")

如果已存在同名 配置文件,它将被关闭。您可以通过省略参数来关闭当前活动(即最后打开)的配置文件。

\Debug\profile()

如果没有活动配置文件,它将打开一个默认的配置文件(命名为 default)。请注意,必须至少打开一个配置文件才能进行调试。

以下代码示例演示了多个调试配置文件的使用

require "../vendor/autoload.php";

// initiate debug profile named 'parser'
\Debug\profile("parser");

\Debug\debug("Parsing some stuff now");
/* Parse the stuff */
\Debug\debug("Stuff successfully parsed");


// initiate debug profile named 'worker'
\Debug\profile("worker");

\Debug\debug("Working with the stuff now");
/* Work with the parsed stuff */
\Debug\debug("Stuff successfully worked with.. or something like that.");

// close the current debug profile (in this case 'worker')
\Debug\profile();


// close the current debug profile (in this case 'parser')
\Debug\profile();

当使用 Web 服务器和浏览器运行上述代码时,将产生类似以下内容:

multiple profiles debug

使用 shell 定义的颜色的输出到 stdout 将大致相同。

时间测量

每条调试行的末尾的毫秒数表示当前调试消息执行与前一条调试消息执行之间的时间差。

此外,每个配置文件持续时间在关闭时测量并显示。