7

In many cases, Xdebug is not suitable for debugging as it involves clicks to run to a particular line of codes. I want to use something that is similar to cakePHP debug function for developers to output the value of a particular property of a class to the browser.

I am using Yii framework and this is my configuration for the yii log in the main.php:

'log'=>array(
    'class'=>'CLogRouter',
        'routes'=>array(
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'trace, info, error, warning, vardump',
        ),
        array(
            'class'=>'CWebLogRoute',
                                'enabled' => YII_DEBUG,
            'levels'=>'error, warning, trace, log, vardump',
            'showInFireBug'=>true,
        ),
    ),
), 

In one of my defined controller i put this code to test:

Yii::log("CallFromUserController",'info', 'application');

However i don't see this being printed in the firebug. I used Chris's example:

http://chris-backhouse.com/advanced-logging-in-yii/775

4

3 回答 3

10

我终于设法找到了解决方案:

在我的 main.php 中,我这样做了:

'log' => array(
    'class' => 'CLogRouter',
    'routes' => array(
        array(
            'class' => 'CFileLogRoute',
            'levels' => 'trace, info, error, warning, vardump',
        ),
        // uncomment the following to show log messages on web pages
        array(
            'class' => 'CWebLogRoute',
            'enabled' => YII_DEBUG,
            'levels' => 'error, warning, trace, notice',
            'categories' => 'application',
            'showInFireBug' => false,
        ),
    ),
),

在我的控制器中,我使用了以下代码:

$a = new array(1,2,3);
Yii::trace(CVarDumper::dumpAsString($a));

应用程序日志显示在每个页面的下方。

于 2013-07-26T08:52:29.050 回答
1

在 Yii2

Yii::trace(VarDumper::dumpAsString($array));

下面给出了自定义日志类的示例。

<?php

namespace app\helpers;

use Yii;
use yii\helpers\VarDumper;

class Log {

    const LOG_CATEGORY_NAME = 'myLog';

    public static function e($msg, $data) {

        if (isset($data)) {
            $msg .= " " . VarDumper::dumpAsString($data);
        }   
        Yii::error($msg, self::LOG_CATEGORY_NAME);
    }   
}

例子:

 Log.e("Copying the Estimate data failed. Model::getError(): ", 
        $model->getErrors());
于 2015-06-06T05:33:07.357 回答
1

对于 debug:true 模式,您应该在 protected/main.php 文件中添加以下行。信任工作就像一个魅力!

return array(
    'preload' => array(
        'debug',
    ),
    'components' => array(
        'debug' => array(
            'class' => 'ext.yii2-debug.Yii2Debug',


         ),
            'db' => array(
                'enableProfiling' => true,
                'enableParamLogging' => true,
            ),
        ),
    );
于 2016-03-25T16:39:27.920 回答