CDbLogRoute
CDbLogRoute stores log messages in a database table.
To specify the database table for storing log messages, set
logTableName as the name of the table and specify
connectionID to be the ID of a
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.
Public Properties
Property |
Type |
Description |
Defined By |
autoCreateLogTable | boolean | whether the log DB table should be automatically created if not exists. | CDbLogRoute |
categories | mixed | array of categories, or string list separated by comma or space. | CLogRoute |
connectionID | string | the ID of CDbConnection application component. | CDbLogRoute |
enabled | boolean | whether to enable this log route. | CLogRoute |
except | mixed | array of categories, or string list separated by comma or space, to EXCLUDE from logs. | CLogRoute |
filter | mixed | the additional filter (eg CLogFilter) that can be applied to the log messages. | CLogRoute |
levels | string | list of levels separated by comma or space. | CLogRoute |
logTableName | string | the name of the DB table that stores log content. | CDbLogRoute |
logs | array | the logs that are collected so far by this log route. | CLogRoute |
Property Details
public boolean $autoCreateLogTable;
whether the log DB table should be automatically created if not exists. Defaults to true.
public string $connectionID;
the ID of CDbConnection application component. If not set, a SQLite database will be automatically created and used. The SQLite database file is protected/runtime/log-YiiVersion.db
.
protected CDbConnection getDbConnection()
the DB connection instance
public string $logTableName;
the name of the DB table that stores log content. Defaults to 'YiiLog'. If 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:
(
id INTEGER NOT NULL PRIMARY KEY,
level VARCHAR(128),
category VARCHAR(128),
logtime INTEGER,
message TEXT
)
Note, the 'id' column must be created as an auto-incremental column. In MySQL, this means it should be
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY
; In PostgreSQL, it is
id SERIAL PRIMARY KEY
.
Method Details
protected void createLogTable(CDbConnection $db, string $tableName) |
$db | CDbConnection | the database connection |
$tableName | string | the name of the table to be created |
Source Code: framework/logging/CDbLogRoute.php#88 (
show)
protected function createLogTable($db,$tableName)
{
$db->createCommand()->createTable($tableName, array(
'id'=>'pk',
'level'=>'varchar(128)',
'category'=>'varchar(128)',
'logtime'=>'integer',
'message'=>'text',
));
}
Creates the DB table for storing log messages.
Source Code: framework/logging/CDbLogRoute.php#103 (
show)
protected function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
elseif(($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);
}
}
Source Code: framework/logging/CDbLogRoute.php#65 (
show)
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);
}
}
}
Initializes the route. This method is invoked after the route is created by the route manager.
protected void processLogs(array $logs) |
$logs | array | list of log messages |
Source Code: framework/logging/CDbLogRoute.php#126 (
show)
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],
));
}
}
Stores log messages into database.