Log Activity in DataBase

In this article i will explain how to customize log activity in database, Imaging you need to save IP user, Url request and user_name in database,But table created by default with Yii only have id,level,category,logtime and message

1: create class file in /protected/extensions/LogDb.php

class LogDb extends CDbLogRoute

    protected function createLogTable($db,$tableName)
        $db->createCommand()->createTable($tableName, array(
            'logtime'=>'timestamp with time zone', 
            'IP_User'=>'varchar(50)', //For IP 
    protected function processLogs($logs)
        $logTime=date('Y-m-d H:i:s'); //Get Current Date

        foreach($logs as $log)
                'IP_User'=> Yii::app()->request->userHostAddress, //Get Ip 
                'user_name'=>Yii::app()->user->name , //Get name
                'request_URL'=>Yii::app()->request->url, // Get Url


and in config/main.php:

                    'levels'=>'error',//You can replace trace,info,warning,error

                // uncomment the following to show log messages on web pages