Log Activity in DataBase

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

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

4 0
Viewed: 31 311 times
Version: Unknown (update)
Category: Tutorials
Written by: mahdi1986
Last updated by: mahdi1986
Created on: Jun 29, 2013
Last updated: 8 years ago
Update Article


View all history

Related Articles