Package | system.logging |
---|---|
Inheritance | class CDbLogRoute » CLogRoute » CComponent |
Since | 1.0 |
Version | $Id$ |
Source Code | framework/logging/CDbLogRoute.php |
Property | Type | Description | Defined By |
---|---|---|---|
autoCreateLogTable | boolean | whether the log DB table should be automatically created if not exists. | CDbLogRoute |
categories | string | list of categories separated by comma or space. | CLogRoute |
connectionID | string | the ID of CDbConnection application component. | CDbLogRoute |
enabled | boolean | whether to enable this log route. | CLogRoute |
filter | mixed | the additional filter (e.g. 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 |
Property | Type | Description | Defined By |
---|---|---|---|
dbConnection | CDbConnection | the DB connection instance | CDbLogRoute |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__destruct() | Destructor. | CDbLogRoute |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
collectLogs() | Retrieves filtered log messages from logger for further processing. | CLogRoute |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
init() | Initializes the route. | CDbLogRoute |
raiseEvent() | Raises an event. | CComponent |
Method | Description | Defined By |
---|---|---|
createLogTable() | Creates the DB table for storing log messages. | CDbLogRoute |
formatLogMessage() | Formats a log message given different fields. | CLogRoute |
getDbConnection() | Returns the DB connection instance | CDbLogRoute |
processLogs() | Stores log messages into database. | CDbLogRoute |
whether the log DB table should be automatically created if not exists. Defaults to true.
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
.
the DB connection instance
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
.
public void __destruct()
|
public function __destruct()
{
if($this->_db!==null)
$this->_db->setActive(false);
}
Destructor. Disconnect the db connection.
protected void createLogTable(CDbConnection $db, string $tableName)
| ||
$db | CDbConnection | the database connection |
$tableName | string | the name of the table to be created |
protected function createLogTable($db,$tableName)
{
$driver=$db->getDriverName();
if($driver==='mysql')
$logID='id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY';
else if($driver==='pgsql')
$logID='id SERIAL PRIMARY KEY';
else
$logID='id INTEGER NOT NULL PRIMARY KEY';
$sql="
CREATE TABLE $tableName
(
$logID,
level VARCHAR(128),
category VARCHAR(128),
logtime INTEGER,
message TEXT
)";
$db->createCommand($sql)->execute();
}
Creates the DB table for storing log messages.
protected CDbConnection getDbConnection()
| ||
{return} | CDbConnection | the DB connection instance |
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);
}
}
public void init()
|
public function init()
{
parent::init();
$db=$this->getDbConnection();
$db->setActive(true);
if($this->autoCreateLogTable)
{
$sql="DELETE FROM {$this->logTableName} WHERE 0=1";
try
{
$db->createCommand($sql)->execute();
}
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 |
protected function processLogs($logs)
{
$sql="
INSERT INTO {$this->logTableName}
(level, category, logtime, message) VALUES
(:level, :category, :logtime, :message)
";
$command=$this->getDbConnection()->createCommand($sql);
foreach($logs as $log)
{
$command->bindValue(':level',$log[1]);
$command->bindValue(':category',$log[2]);
$command->bindValue(':logtime',(int)$log[3]);
$command->bindValue(':message',$log[0]);
$command->execute();
}
}
Stores log messages into database.
Signup or Login in order to comment.