Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.3 KiB

13 years ago
<?php
/**
13 years ago
* DbTarget class file.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
13 years ago
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\logging;
13 years ago
/**
13 years ago
* DbTarget stores log messages in a database table.
13 years ago
*
13 years ago
* By default, DbTarget will use the database specified by [[connectionID]] and save
* messages into a table named by [[tableName]]. Please refer to [[tableName]] for the required
* table structure. Note that this table must be created beforehand. Otherwise an exception
* will be thrown when DbTarget is saving messages into DB.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class DbTarget extends Target
13 years ago
{
/**
13 years ago
* @var string the ID of [[\yii\db\dao\Connection]] application component.
* Defaults to 'db'. Please make sure that your database contains a table
* whose name is as specified in [[tableName]] and has the required table structure.
* @see tableName
13 years ago
*/
13 years ago
public $connectionID = 'db';
13 years ago
/**
13 years ago
* @var string the name of the DB table that stores log messages. Defaults to '{{log}}'.
* If you are using table prefix 'tbl_' (configured via [[\yii\db\dao\Connection::tablePrefix]]),
* it means the DB table would be named as 'tbl_log'.
*
* The DB table must have the following structure:
*
* ~~~
* CREATE TABLE tbl_log (
* id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
* level VARCHAR(32),
* category VARCHAR(255),
* log_time INTEGER,
* message TEXT,
* INDEX idx_log_level (level),
* INDEX idx_log_category (category)
* )
* ~~~
*
* Note that the 'id' column must be created as an auto-incremental column.
* The above SQL shows the syntax of MySQL. If you are using other DBMS, you need
* to adjust it accordingly. For example, in PosgreSQL, it should be `id SERIAL PRIMARY KEY`.
*
* The indexes declared above are not required. They are mainly used to improve the performance
* of some queries about message levels and categories. Depending on your actual needs, you may
* want to create other indexes.
13 years ago
*/
13 years ago
public $tableName = '{{log}}';
13 years ago
13 years ago
private $_db;
13 years ago
/**
13 years ago
* Returns the DB connection used for saving log messages.
* @return \yii\db\dao\Connection the DB connection instance
* @throws \yii\base\Exception if [[connectionID]] does not refer to a valid application component ID.
13 years ago
*/
13 years ago
public function getDbConnection()
13 years ago
{
if ($this->_db === null) {
$this->_db = \Yii::$application->getComponent($this->connectionID);
if (!$this->_db instanceof \yii\db\dao\Connection) {
throw new \yii\base\Exception('DbTarget.connectionID must refer to a valid application component ID');
}
13 years ago
}
return $this->_db;
13 years ago
}
/**
13 years ago
* Stores log [[messages]] to DB.
* @param boolean $final whether this method is called at the end of the current application
13 years ago
*/
13 years ago
public function exportMessages($final)
13 years ago
{
13 years ago
$sql = "INSERT INTO {$this->tableName}
(level, category, log_time, message) VALUES
(:level, :category, :log_time, :message)";
13 years ago
$command = $this->getDbConnection()->createCommand($sql);
13 years ago
foreach ($this->messages as $message) {
$command->bindValues(array(
':level' => $message[1],
':category' => $message[2],
':log_time' => $message[3],
':message' => $message[0],
))->execute();
13 years ago
}
13 years ago
$this->messages = array();
13 years ago
}
}