8

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

2
  • 1
    You don't have the 'info' level (which is what you specify as the second argument to Yii::log()) enabled for CWebLogRoute so of course what you try won't work. Commented Jul 26, 2013 at 20:46
  • Ok, i tried it too but it didn't work. The below solution worked for me though :) Commented Jul 27, 2013 at 4:13

3 Answers 3

11

I finally managed to find out a solution:

In my main.php I did this:

'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,
        ),
    ),
),

In my controller I used this code:

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

The Application Log is shown below in every page.

Sign up to request clarification or add additional context in comments.

Comments

1

In Yii2

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

An example of customized log class is given below.

<?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);
    }   
}

Example:

 Log.e("Copying the Estimate data failed. Model::getError(): ", 
        $model->getErrors());

Comments

1

You should add following lines to your protected/main.php file for debug:true mode. Trust work like a charm !

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


         ),
            'db' => array(
                'enableProfiling' => true,
                'enableParamLogging' => true,
            ),
        ),
    );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.