Yii 1.1: fdbweblogroute

Log SQL queries to web page and beautify the SQL a bit

The FDbWebLogRoute is a CWebLogRoute extension solely for logging SQL queries. While you can log ALL the SQL using Yii profiling (See http://www.yiiframework.com/doc/guide/1.1/en/topics.logging#profiling-sql-executions) this could be prettier with FDbWebLogRoute because the profiler always gives you a start and a end log for each query. It means you see one query twice.

This logroute also use Google code prettier JS library to beautify the SQLs a bit. Some color and highlighting to save your eyes.


Google code prettier library. This is not distributed with the extension. Download is here: http://code.google.com/p/google-code-prettify/


The intension of this extension is for debugging your SQL. If you need profiling, identify the bottleneck SQL, do use Yii profiling.

To log a query:


Same way of dbLogBegin/End wrapping your code to log all queries executed in the code block. i.e. put these function at the begin and end of your controller's action.


Put this file under extensions/logging/ folder

Put Google code prettify library under extensions/scripts/code.prettify (rename the 'src' folder in your download by 'code.prettify'). If this is not what you want, edit line #58, #59 in FDbWebLogRoute.php


// db component in config/main.php
        'db' => array(
            'enableProfiling'=>true, //optional, if you also need profiling
            'enableParamLogging'=>true, //to show parameter values
// log component in config/main.php
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                'dbLog' => array(
                    'class' => 'ext.logging.FDbWebLogRoute', //if you put this file under extensions/logging/ fodler

Total 3 comments

#6279 report it
nsanden at 2011/12/23 11:16pm
Definitely potential

I also had some issues getting this to work. Doesn't seem like Yii is publishing the prettify code correctly. No js or css file got created in my assets/a324234/scripts/code.prettify folder (but it did create the folder?). Who knows, i'm still new to Yii. So I moved those 2 files to that folder and it works.

Side note, this worked nice for making the SQL queries even easier to read.


$('.pln:contains("WHERE")').before('<br />');
$('.pln:contains("SELECT")').before('<br />');
$('.pln:contains("FROM")').before('<br />');
$('.pln:contains("LEFT")').before('<br />');
$('.pln:contains("INNER")').before('<br />');
$('.pln:contains("LIMIT")').before('<br />');
$('.pln:contains("ORDER")').before('<br />');


after line 63 of FDbWebLogRoute.php

#2463 report it
Hudson Nguyen at 2011/01/06 01:10am
Re: Looks like it has good potential

You're right about the prettify location.

The log routes usually don't need to have name but there is nothing wrong with it. The dbLog is a key of DB log route we need to refer to in the code. Yii doesn't care whether the log route configuration use numeric (by default) or string key.

#2449 report it
gsatir at 2011/01/04 01:55am
Looks like it has good potential

I haven't been able to get this to work reliably. There are some gaps in the documentation here that could be clarified. First: put the Google library under extensions/assets/scripts/code.prettify

Also, the deployment is not clear. Why is there a 'dbLog'=> in the routes config? routes don't usually look like that. See here. It makes me think there is perhaps something else missing?

Nice start here.


Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: Hudson Nguyen
  • Category: Logging
  • Votes: +3 / -1
  • Downloaded: 538 times
  • Created on: Dec 30, 2010
  • Last updated: never
  • Tags: db, log, sql log