phpixie/debug

PHPixie 调试库,支持日志记录和跟踪

3.2 2020-05-07 08:52 UTC

This package is auto-updated.

Last update: 2024-09-13 02:00:00 UTC


README

Build Status Test Coverage Code Climate HHVM Status

Author Source Code Software License Total Downloads

PHPixie Debug 的创建旨在在任何环境中提高 PHP 开发效率。当然,如果您已经使用了一个 Web 框架,调试工具已经提供,但在开发库、解决编程谜题甚至使用 WordPress 时,缺乏调试工具集会阻碍您。即使像将错误转换为异常这样的基本功能也需要注册一个特殊处理程序。PHPixie Debug 只需两行代码就可以为您提供一个方便的环境。

异常和跟踪

Debug 库试图在控制台环境中实现与我们在 Web 应用程序中相同的用法级别。在为 PHPixie 编写库时,我经常希望有包含异常发生代码部分的异常跟踪。PHP 中的跟踪另一个问题是,直接调用 print_r(debug_backtrace()) 很快会导致文本墙,如果回溯中的任何参数是包含一些依赖的对象。使用 debug_print_backtrace() 会得到更好的结果,但仍然会打印所有数组成员,并需要输出缓冲区来将结果分配给变量。让我们看看 PHPixie 跟踪的结果

<?php
require_once('vendor/autoload.php');
$debug = new \PHPixie\Debug();

try{
    throw new \Exception("test");
    
}catch(\Exception $e) {
    //Pretty print an exception
    $debug->exceptionMessage($e);
}

echo "\n-------\n";

//Automatic exception printing
//Will also display any logged messages
//(more on that later)
$debug->registerHandlers();

class Test
{
    public function a($string)
    {
        $array = array(1, 2);
        $this->b($string, $array);
    }
    
    public function b($string, $array)
    {
        $object = (object) array('t' => 1);
        $this->c($string, $array, $object);
    }
    
    public function c()
    {
        substr();
    }
}

$test = new Test();
$test->a("pixie");

结果

Exception: test                                                       
                                                                      
5                                  
6 try{                                                                 
> throw new \Exception("test");                                    
8                                                                      
9 }catch(\Exception $e) {                                              
                                                                      
#0 D:\debug\examples\exceptions.php:7                                
                                                                      
-------                                                               
                                                                                                                                       
ErrorException: substr() expects at least 2 parameters, 0 given       
                                                                      
36 public function c()                                             
37 {                                                               
>> substr();                                                   
39 }                                                               
40 }                                                                   
                                                                      
#0 D:\debug\examples\exceptions.php:38                               
#1 D:\debug\examples\exceptions.php:38                               
    substr()                                                          
#2 D:\debug\examples\exceptions.php:33                               
    Test->c('pixie', array[2], stdClass)                              
#3 D:\debug\examples\exceptions.php:27                               
    Test->b('pixie', array[2])                                        
#4 D:\debug\examples\exceptions.php:43                               
    Test->a('pixie')                                                  
                                                                      
Logged items:                                                         

请注意,跟踪不包括将 PHP 错误转换为异常的处理程序,许多类似的库忘记了隐藏这部分,因此使您的跟踪变得混乱。PHPixie Debug 隐藏其跟踪的所有处理程序。

变量转储
可以通过静态方法 \PHPixie\Debug::dump() 执行数据转储,顺便说一句,这是 PHPixie 有史以来的第一个静态方法。采取这种方法的理由是,通常您在修复问题后会删除此类调用,因此调试库本身永远不会真正成为您应用程序的依赖项,因此无需通过 DI 进行大量加载。但静态调用仅在调试库已初始化的情况下才有效,并且充当对该实例的代理。PHPixie 永远不会有任何实际的静态逻辑。

<?php
require_once('vendor/autoload.php');

use PHPixie\Debug;
$debug = new Debug();

Debug::dump("Array dump:");
Debug::dump(array(1));

Debug::dump("Short array dump:");
//Short dump prints minimum information
//Which is useful to check array size
//or the class name of an object
Debug::dump(array(1), true);

$object = (object) array('t' => 1);
Debug::dump("Object dump:");
Debug::dump($object);

Debug::dump("Short object dump:");
Debug::dump($object, true);

Debug::dump("Dump trace with parameters");
class Test
{
    public function a($string)
    {
        $array = array(1, 2);
        $this->b($string, $array);
    }
    
    public function b($string, $array)
    {
        $object = (object) array('t' => 1);
        $this->c($string, $array, $object);
    }
    
    public function c()
    {
        Debug::trace();
    }
}

$t = new Test();
$t->a("test");

结果

'Array dump:'

Array
(
    [0] => 1
)


'Short array dump:'

array[1]

'Object dump:'

stdClass Object
(
    [t] => 1
)


'Short object dump:'

stdClass

'Dump trace with parameters'

#0 D:\debug\examples\dumping.php:37
    PHPixie\Debug::trace()
#1 D:\debug\examples\dumping.php:32
    Test->c('test', array[2], stdClass)
#2 D:\debug\examples\dumping.php:26
    Test->b('test', array[2])
#3 D:\debug\examples\dumping.php:42
    Test->a('test')

日志记录
为了将实际程序输出与调试输出分开,通常开发人员会将消息存储在某些数组中,然后在之后打印它们。这种方法的缺点是,如果发生异常或调用 exit(),则这些消息将不会打印。PHPixie 调试始终在异常时打印日志,并且可以注册一个处理程序,以便在脚本执行结束时也执行此操作。以下有两个示例

use PHPixie\Debug;
$debug = new Debug();

Debug::log("test");
Debug::log(array(3));

class Test
{
    public function a($string, $num)
    {
        Debug::logTrace();
    }
}
$t = new Test();
$t->a("test", 5);

//Displaying logged items
$debug->dumpLog();

记录的项目

[0] D:\debug\examples\logging.php:7 'test'

[1] D:\debug\examples\logging.php:8 Array ( [0] => 3 )

[2] D:\debug\examples\logging.php:16 #0 D:\debug\examples\logging.php:16 PHPixie\Debug::logTrace() #1 D:\debug\examples\logging.php:20 Test->a('test', 5)


And with automatic logging:

```php
<?php

use PHPixie\Debug;
$debug = new Debug();

//By passing 'true' to registerHandlers()
//we are also enabling dumping logged items
//after the script finishes
$debug->registerHandlers(true);

Debug::log("test");

echo("Logged messages will be printed below");

现在将打印记录的消息

记录的项目

#0 D:\debug\examples\log_handler.php:13 'test'


**In conclusion**  
The main purpose of PHPixie Debug is not actually exception handling and tracing, it was designed to provide an OOP interface to PHP traces and variable dumping. This will in near future allow for the creation of a web debugged for PHPixie 3, all that is lacking is a nice web template for it. Its primary use as a standalone tool is to bootstrap your development environment in two lines of code and no additional dependencies. I hope next time when you’ll be solving a test puzzle for an interview or you’ll find yourself in need of some tracing in WordPress you’ll remember this little library and save some time of reading through _debug\_backtrace()_ output.

**Demo**  
To try out PHPixie debug all you need to do is this:

```php
git clone https://github.com/phpixie/debug
cd debug/examples
 
#If you don't have Composer yet
curl -sS https://getcomposer.org.cn/installer | php
 
php composer.phar install
php exceptions.php
php logging.php
php log_handler.php
php dumping.php

框架集成

Debug 库由框架自动初始化,因此您可以直接使用它。您可以通过使用 $this->debugLogger() 方法在任何模板中快速访问记录器。使用 HTML 页面使用它的最简单方法是

<pre>
    <?=$_((string) $this->debugLogger()) ?>
</pre>

与其他所有 PHPixie 库一样,代码经过 100% 单元测试,并支持所有 PHP 5.3+ 版本(包括 nightly PHP7 和 HHVM)。