1、修改yii框架的配置文件(main.php)php
'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( 'class' => 'AppCDbLogRoute', 'connectionID' => 'db', 'levels' => 'info, warning, error, profile, debug', 'logTableName' => 'common_app_logs', ), ), ),
2、創建保存yii日誌的數據表數據庫
CREATE TABLE IF NOT EXISTS `common_app_logs` ( `id` int(11) unsigned NOT NULL COMMENT '自增加id', `level` varchar(128) DEFAULT NULL COMMENT '錯誤級別', `type` varchar(10) NOT NULL COMMENT '後臺類型(op,sup)', `category` varchar(128) NOT NULL COMMENT '所屬分類', `action` varchar(128) NOT NULL COMMENT '所屬action', `urlparam` text NOT NULL COMMENT '當前url', `ip` varchar(20) DEFAULT '' COMMENT '來訪者ip', `postparam` text NOT NULL COMMENT 'post參數', `logtime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '日誌記錄時間', `message` text NOT NULL COMMENT '日誌記錄信息' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='應用層日誌記錄表' AUTO_INCREMENT=1 ;
數據庫表有我本身自定義的字段,本來的字段能夠參考YII源碼中的CDbLogRoute.php文件(/framework/logging下)app
3、複製CLogRoute文件,更名爲AppCDbLogRoute放到到組件(conponents)中,繼承CLogRoute這個類框架
修改文件中createLogTable方法,這個方法是若是沒有這個表就創建,數據表若是已經創建,這個能夠忽略。yii
原方法:post
protected function createLogTable($db,$tableName) { $db->createCommand()->createTable($tableName, array( 'id'=>'pk', 'level'=>'varchar(128)', 'category'=>'varchar(128)', 'logtime'=>'integer', 'message'=>'text', )); }
修改爲:ui
protected function createLogTable($db,$tableName) { $db->createCommand()->createTable($tableName, array( 'id'=>'pk', 'level'=>'varchar(128)', 'type'=>'varchar(10)', 'category'=>'varchar(128)', 'action'=>'varchar(128)', 'urlparam'=>'text', 'postparam'=>'text', 'logtime'=>'integer', 'message'=>'text', )); }
修改方法processLogsthis
原方法:url
protected function processLogs($logs) { $command=$this->getDbConnection()->createCommand(); foreach($logs as $log) { $command->insert($this->logTableName,array( 'level'=>$log[1], 'category'=>$log[2], 'logtime'=>(int)$log[3], 'message'=>$log[0], )); } }
新方法:spa
protected function processLogs($logs) { $command = $this->getDbConnection()->createCommand(); foreach ($logs as $log) { $action = explode('?', Yii::app()->request->url); $command->insert($this->logTableName, array( 'level' => $log[1], 'type' => 'op', 'category' => $log[2], 'logtime' => (int) $log[3], 'message' => $log[0], 'action' => $action[0], 'urlparam' => Yii::app()->request->getHostInfo().Yii::app()->request->url, 'ip' => getIp(), 'postparam' => http_build_query($_POST), )); } }
4、寫日誌
在代碼的任何位置加入
Yii::log('log test','info','mail');
這樣就能寫入到數據庫中。
5、附錄
這裏有自定義的獲取ip的方法
function getIp() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { $ip = getenv("HTTP_CLIENT_IP"); } elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { $ip = getenv("REMOTE_ADDR"); } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { $ip = $_SERVER['REMOTE_ADDR']; } else { $ip = "0"; } //有時候代理會顯示出來多個IP這裏只取第一個作判斷 $pos = strpos($ip, ','); if ($pos !== false) { $ip = substr($ip, 0, $pos); } return $ip; }