CDbLogRoute.php
3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* CDbLogRoute class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbLogRoute stores log messages in a database table.
*
* To specify the database table for storing log messages, set {@link logTableName} as
* the name of the table and specify {@link connectionID} to be the ID of a {@link CDbConnection}
* application component. If they are not set, a SQLite3 database named 'log-YiiVersion.db' will be created
* and used under the application runtime directory.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.logging
* @since 1.0
*/
class CDbLogRoute extends CLogRoute
{
/**
* @var string the ID of CDbConnection application component. If not set, a SQLite database
* will be automatically created and used. The SQLite database file is
* <code>protected/runtime/log-YiiVersion.db</code>.
*/
public $connectionID;
/**
* @var string the name of the DB table that stores log content. Defaults to 'YiiLog'.
* If {@link autoCreateLogTable} is false and you want to create the DB table manually by yourself,
* you need to make sure the DB table is of the following structure:
* <pre>
* (
* id INTEGER NOT NULL PRIMARY KEY,
* level VARCHAR(128),
* category VARCHAR(128),
* logtime INTEGER,
* message TEXT
* )
* </pre>
* Note, the 'id' column must be created as an auto-incremental column.
* In MySQL, this means it should be <code>id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY</code>;
* In PostgreSQL, it is <code>id SERIAL PRIMARY KEY</code>.
* @see autoCreateLogTable
*/
public $logTableName='YiiLog';
/**
* @var boolean whether the log DB table should be automatically created if not exists. Defaults to true.
* @see logTableName
*/
public $autoCreateLogTable=true;
/**
* @var CDbConnection the DB connection instance
*/
private $_db;
/**
* Initializes the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
parent::init();
if($this->autoCreateLogTable)
{
$db=$this->getDbConnection();
try
{
$db->createCommand()->delete($this->logTableName,'0=1');
}
catch(Exception $e)
{
$this->createLogTable($db,$this->logTableName);
}
}
}
/**
* Creates the DB table for storing log messages.
* @param CDbConnection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected function createLogTable($db,$tableName)
{
$db->createCommand()->createTable($tableName, array(
'id'=>'pk',
'level'=>'varchar(128)',
'category'=>'varchar(128)',
'logtime'=>'integer',
'message'=>'text',
));
}
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
protected function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
else if(($id=$this->connectionID)!==null)
{
if(($this->_db=Yii::app()->getComponent($id)) instanceof CDbConnection)
return $this->_db;
else
throw new CException(Yii::t('yii','CDbLogRoute.connectionID "{id}" does not point to a valid CDbConnection application component.',
array('{id}'=>$id)));
}
else
{
$dbFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'log-'.Yii::getVersion().'.db';
return $this->_db=new CDbConnection('sqlite:'.$dbFile);
}
}
/**
* Stores log messages into database.
* @param array $logs list of log messages
*/
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],
));
}
}
}