Difference between #3 and #4 of How to log and debug variables using CWebLogRoute

unchanged
Title
How to log and debug variables using CWebLogRoute
unchanged
Category
How-tos
unchanged
Tags
Logging
changed
Content
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
](http://www.yiiframework.com/doc/guide/1.1/en/topics.logging#message-logging
"logging messages")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](http://www.yiiframework.com/doc/api/1.1/CWebLogRoute
"CWebLogRoute") to create a Yii version of firePHP.

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

~~~
[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'=>'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:

~~~
[php]
$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](http://www.yiiframework.com/wiki/31/use-shortcut-functions-to-reduce-typing/
"use shortcut functions to reduce typing"). We are going to write a
function in our index.php page exactly as that of firePHP:

~~~
[php]
//
// In your index.php or your globals.php file
function fb($what){
  echo
Yii::trace(CVarDumper::dumpAsString($what),'vardump');Yii::trace(CVarDumper::dumpAsString($this),'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.

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