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