Yii-how to use CWebLogRoute to record and debug variables

Source: Internet
Author: User
I have read several times about using external libraries to Debug PHP code (for example, firePHP). after reading this article, you will find that these external libraries are not necessary in Yii.

I have read several times about using external libraries to Debug PHP code (such as firePHP). after reading this article, you will find that these external libraries are not necessary in Yii.

Yii has a built-in powerful logging class. if you read the log recording document, you can find that we can decide the log we want to record. this is exactly what we want to do. use CWebLogRoute to create a Yii version of FirePHP.

Configuration

Add the configuration in our protected/config/main. php configuration 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

After the preparation is complete, let's track the variables and test them as follows:

$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'); // arrayecho Yii::trace(CVarDumper::dumpAsString($anotherTest),'vardump'); // objectecho Yii::trace(CVarDumper::dumpAsString($this),'vardump');
FirePHP functions of Yii

The above code has been written for a long time. let's use some strong suggestions and let me write a function like FirePHP on the index. php page:

//// In your index.php or your globals.php filefunction fb($what){  echo Yii::trace(CVarDumper::dumpAsString($what),'vardump');} // // using the above examples now we could$test = 'This is a test'; fb($test);

Okay, all done. we didn't use external classes in the debugger.

Supplement

I forgot to mention it. it also applies to the Chrome development tool console.

How to log and debug variables using CWebLogRoute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.