yii 如何使用 CWebLogRoute 记录和调试变量

2 篇文章 0 订阅

简介

我看了几遍关于使用外部库来调试PHP代码(如: firePHP)的文章, 读了这篇文章你会发现在 Yii 中没有必要使用这些外部库.
Yii 内置了强大的日志记录类. 如果你阅读了记录日志的文档, 你可以发现我们可以决定我们希望记录的日志, 这正是我们要做的,使用 CWebLogRoute 创建一个 Yii 版本的 FirePHP.

配置

在我们的 protected/config/main.php 配置文件中添加配置:

'log'=>array(
    'class'=>'CLogRouter',
    'routes'=>array(
        array(
            'class'=>'CWebLogRoute',
            //
            // I include *trace* for the
            // sake of the example, you can include
            //
            // more levels separated by commas
            'levels'=>'trace', 
            //
            // I include *vardump* but you
            // can include more separated by commas
            'categories'=>'application.*,system.db.*,vardump', 
            //
            // This is self-explanatory right?
             'showInFireBug'=>true ), 
        ),
),

使用

准备完毕,现在让我们来追踪一下变量来测试一下, 如下:

public function actionTrace(){
    $test = 'This is a test';

    $anotherTest = array('one','two','three');

    echo Yii::trace(CVarDumper::dumpAsString($test),'vardump');

    echo Yii::trace(CVarDumper::dumpAsString($anotherTest),'vardump');

    echo Yii::trace(CVarDumper::dumpAsString($this),'vardump');
}

Yii 的 FirePHP 函数

上面的代码写起来比较长, 我们来使用一下 强的建议, 让我在 index.php 页面写一个像 FirePHP 函数:

public function actionTrace()
{
    //
    // In your index.php or your globals.php file
    function fb($what)
    {
        echo Yii::trace(CVarDumper::dumpAsString($what), 'vardump');
    }

    // 
    // using the above examples now we could
    $test = 'This is a test';

    fb($test);
}

OK, 全部完成了,我们的调试器中没有使用外部的类.

补充

此方法无需安装chromephp 插件,firebug浏览器均可使用,opera,chrome,firefox等都适用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值