Yii 1.1: How to log and debug variables using CWebLogRoute

22 followers

Introduction

I have seen a couple of articles about how to integrate external libraries to debug our PHP code (i.e. firePHP) but after you read this article you will realize that there is no need for such libraries when using Yii.

Yii comes with a set of powerful classes for logging. If you have read the documentation about logging messages, you have noticed that we can actually decide which messages we wish to log. And this is exactly what we are going to do, using CWebLogRoute to create a Yii version of firePHP.

Configuration

Include the magic settings on our protected/config/main.php file:

'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'=>'vardump',
        //
        // This is self-explanatory right?
    'showInFireBug'=>true
),

Use

Ready to go, lets going to trace some vars to test it, this is how to do it:

$test = 'This is a test';
 
$anotherTest = array('one','two','three');
 
// variable
// please, check the inclusion of the category *vardump*
// not including the category, it wont display at all.
echo Yii::trace(CVarDumper::dumpAsString($test),'vardump');
 
// array
echo Yii::trace(CVarDumper::dumpAsString($anotherTest),'vardump');
 
// object
echo Yii::trace(CVarDumper::dumpAsString($this),'vardump');

Yii's firePHP function

But the above code is too long to write, lets use Qiang's suggestion. We are going to write a function in our index.php page exactly as that of firePHP:

//
// 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);

And that's it; our nice Yii PHP variable debugger and the best of all, no external libraries included.

Addendum

I forgot to mention that it also works on Chrome developer tools console.

Total 7 comments

#17791 report it
jb.bhavik at 2014/07/23 01:05am
List catagories

can you pls list all the catagories of CWebLogRoute. Cos I'm new to Yii And many thanks for this wiki..

#15908 report it
Nabi at 2013/12/28 12:55pm
Integrating FirePHP is better of this

I thinks it is not great, and use var_dump directly in view is better. And so, this is not support of ajax request. Also, I thinks use Integrating FirePHP is great. here: http://www.yiiframework.com/wiki/84/integrating-firephp

#15054 report it
Mathew Gor at 2013/10/02 03:21pm
Never

add stuff into your index.php If needed, create a file and include(import) it.

#8830 report it
rszemeti at 2012/07/01 06:18pm
Careful with that AJAX Eugene!

logging to firebug etc is fine ... and I was enjoying logging all db calls to firebug, and being able to diagnose how Yii was using the db directly into FireBug ... but it destroys AJAX and similar functionality, so half my pages flaked .. which sort of makes its usefulness as a debug tool limited .. if someone could figure out how to have it turned off for AJAX mode .. that would help.

#5774 report it
drumaddict at 2011/11/11 04:57am
Cool

I got depressed recently when my firephp extension stopped working with latest firefox/firebug.This is a nice solution I was unaware off,just tried it and works perfectly. What I miss though,I'd like to see the name of the variable in my console(besides the value),and I cant find a way to do it.The second argument of trace will not accept any string...

#2513 report it
Antonio Ramirez at 2011/01/12 05:53pm
@trej

Hi Trej, thanks for commenting... I used to that also, but if you try to compare the way var_dump works and the way dumpAsString does render the variables, you will see the difference between both approaches.

Also, for a guy like me that uses extensively AJAX, var_dump is a very bad option and to develop a script that writes to console without interfering normal AJAX calls isn't as easy as it seems, as I used to normally had to encode within the success, failure, complete... and so on, js functions.

Again, all options are depending on the way a programmer feels comfortable with.

#2453 report it
Trejder at 2011/01/04 11:04am
What about echo()...

A very good article, Antonio, but to be honest the fastest way (at least) for me to debug some variable is using var_dump, print_r or echo directly in a view! :]

Leave a comment

Please to leave your comment.

Write new article
  • Written by: Antonio Ramirez
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +18
  • Viewed: 48,591 times
  • Created on: Dec 11, 2010
  • Last updated: Dec 12, 2010
  • Tags: Logging