How to display page profiling results in Yii

In your /protected/config/main.php, update the variables [‘db’][‘enableProfiling’] as well as a add CProfileLogRoute class. It should look like the following:

[code lang=”php”] array(
“enableProfiling” => true;
“connectionString” => “mysql:host=localhost;dbname=db_name”,
“emulatePrepare” => true,
“enableParamLogging” => true,
“username” => “root”,
“password” => “”,
“charset” => “utf8″,
“log” => array(
“class” => “CLogRouter”,
“routes” => array(
array(“class” => “CProfileLogRoute”),
array(“class” => “CFileLogRoute”, “levels” => “error, warning, trace”,),

This is for the PHP framework Yii Framework.

How to log slow MySQL queries

I haven’t been generous enough to spend money on higher RAM for my blogs. I started to notice slow downs in the blog recent, particularly caused by a particular plugin that does related posts. I found out by checking on slow queries log. Here’s how to get MySQL to log slow queries:

If you are using Ubuntu of Debian-based operating systems, it’s found in /etc/mysql/my.cnf. You may need to use

[code lang=”bash”]sudo nano /etc/mysql/my.cnf[/code]

Press CTRL + W, that’s search in nano. Find “slow” for the section for logging slow queries.

[code lang=”bash”]# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2

I uncommented “log_slow_queries” and “long_query_time”. I put long_query_time to 8. That requires a bit of experimentation. It depends on your environment.

I run the following command to check the slow queries:

[code lang=”bash”]sudo cat /var/log/mysql/mysql-slow.log[/code]

After you change the configuration in my.cnf, you need to restart. You can do so using:

[code lang=”bash”]sudo /etc/init.d/mysql restart[/code]

Hope it helps.

Remember to clear your Apache logs

This is what happens if you don’t clear your Apache logs:

[code lang=”bash”]jr ~/ ls -al
total 205748
drwxr-s— 2 me www-data 4096 Aug 5 2008 .
drwxr-s— 8 me www-data 4096 Sep 9 14:58 ..
-rw-r–r– 1 root www-data 210250161 Feb 21 17:08 access.log
-rw-r–r– 1 root www-data 205822 Feb 21 15:49 error.log[/code]

That’s 210 MB of data I am least interested with. By the way I just realized delete the log actually lowers memory usage significantly.

How to do logging in CakePHP

Okay, I realized this long ago but it annoys me that every time I just forget this. Sometime, when debugging CakePHP, the most direct method is to simply log an error. It’s like ‘trace’ in Actionscript or just a println() in Java. Anyway, here it is:

[code lang=”php”]$this->log(“Log anything here.”);[/code]

This can be called from the controller. Continue reading How to do logging in CakePHP