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.

92 lines
2.8 KiB

14 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\log;
14 years ago
use Yii;
use yii\db\Connection;
use yii\base\InvalidConfigException;
14 years ago
/**
14 years ago
* DbTarget stores log messages in a database table.
14 years ago
*
* By default, DbTarget stores the log messages in a DB table named 'tbl_log'. This table
* must be pre-created. The table name can be changed by setting [[logTable]].
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class DbTarget extends Target
14 years ago
{
/**
* @var Connection|string the DB connection object or the application component ID of the DB connection.
* After the DbTarget object is created, if you want to change this property, you should only assign it
* with a DB connection object.
14 years ago
*/
public $db = 'db';
14 years ago
/**
* @var string name of the DB table to store cache content.
* The table should be pre-created as follows:
14 years ago
*
* ~~~
* CREATE TABLE tbl_log (
* id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
* level INTEGER,
14 years ago
* 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 uses the MySQL syntax. If you are using other DBMS, you need
* to adjust it accordingly. For example, in PostgreSQL, it should be `id SERIAL PRIMARY KEY`.
14 years ago
*
* 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 additional indexes (e.g. index on `log_time`).
14 years ago
*/
public $logTable = '{{%log}}';
14 years ago
/**
* Initializes the DbTarget component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
14 years ago
*/
public function init()
14 years ago
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbTarget::db must be either a DB connection instance or the application component ID of a DB connection.");
14 years ago
}
}
/**
* Stores log messages to DB.
14 years ago
*/
12 years ago
public function export()
14 years ago
{
$tableName = $this->db->quoteTableName($this->logTable);
$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[message]])
VALUES (:level, :category, :log_time, :message)";
$command = $this->db->createCommand($sql);
12 years ago
foreach ($this->messages as $message) {
$command->bindValues([
14 years ago
':level' => $message[1],
':category' => $message[2],
':log_time' => $message[3],
':message' => $message[0],
])->execute();
14 years ago
}
}
}