首页 > 代码库 > yii日志保存机制

yii日志保存机制

一、修改yii框架的配置文件(main.php)

        ‘log‘ => array(            ‘class‘ => ‘CLogRouter‘,            ‘routes‘ => array(                array(                    ‘class‘ => ‘AppCDbLogRoute‘,                    ‘connectionID‘ => ‘db‘,                    ‘levels‘ => ‘info, warning, error, profile, debug‘,                    ‘logTableName‘ => ‘common_app_logs‘,                ),            ),        ), 

二、建立保存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下)

三、复制CLogRoute文件,改名为AppCDbLogRoute放到到组件(conponents)中,继承CLogRoute这个类

修改文件中createLogTable方法,这个方法是如果没有这个表就建立,数据表如果已经建立,这个可以忽略。

原方法:

    protected function createLogTable($db,$tableName)    {        $db->createCommand()->createTable($tableName, array(            ‘id‘=>‘pk‘,            ‘level‘=>‘varchar(128)‘,            ‘category‘=>‘varchar(128)‘,            ‘logtime‘=>‘integer‘,            ‘message‘=>‘text‘,        ));    }

 

修改成:

    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‘,        ));    }

修改方法processLogs

原方法:

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],   ));  } }

新方法:

    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),            ));        }    }

 

四、写日志

在代码的任何位置加入

Yii::log(‘log test‘,‘info‘,‘mail‘);

这样就能写入到数据库中。

五、附录

这里有自定义的获取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;}

 

yii日志保存机制