DbTarget.php 2.93 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8
namespace yii\logging;
w  
Qiang Xue committed
9

10
use Yii;
Qiang Xue committed
11 12 13
use yii\db\Connection;
use yii\base\InvalidConfigException;

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15
 * DbTarget stores log messages in a database table.
w  
Qiang Xue committed
16
 *
17 18
 * 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]].
w  
Qiang Xue committed
19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
21
 * @since 2.0
w  
Qiang Xue committed
22
 */
w  
Qiang Xue committed
23
class DbTarget extends Target
w  
Qiang Xue committed
24 25
{
	/**
26 27 28
	 * @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.
w  
Qiang Xue committed
29
	 */
30
	public $db = 'db';
w  
Qiang Xue committed
31
	/**
32 33
	 * @var string name of the DB table to store cache content.
	 * The table should be pre-created as follows:
w  
Qiang Xue committed
34 35 36
	 *
	 * ~~~
	 * CREATE TABLE tbl_log (
37
	 *	   id       BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Qiang Xue committed
38
	 *	   level    INTEGER,
w  
Qiang Xue committed
39 40 41 42 43 44 45 46 47
	 *	   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.
48
	 * The above SQL uses the MySQL syntax. If you are using other DBMS, you need
Qiang Xue committed
49
	 * to adjust it accordingly. For example, in PostgreSQL, it should be `id SERIAL PRIMARY KEY`.
w  
Qiang Xue committed
50 51 52
	 *
	 * 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
53
	 * want to create additional indexes (e.g. index on `log_time`).
w  
Qiang Xue committed
54
	 */
55
	public $logTable = 'tbl_log';
w  
Qiang Xue committed
56 57

	/**
58 59 60
	 * 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.
w  
Qiang Xue committed
61
	 */
62
	public function init()
w  
Qiang Xue committed
63
	{
64 65 66 67 68 69
		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.");
w  
Qiang Xue committed
70
		}
Qiang Xue committed
71 72
	}

w  
Qiang Xue committed
73
	/**
Qiang Xue committed
74 75 76
	 * Stores log messages to DB.
	 * @param array $messages the messages to be exported. See [[Logger::messages]] for the structure
	 * of each message.
w  
Qiang Xue committed
77
	 */
Qiang Xue committed
78
	public function export($messages)
w  
Qiang Xue committed
79
	{
80
		$tableName = $this->db->quoteTableName($this->logTable);
81 82
		$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[message]])
				VALUES (:level, :category, :log_time, :message)";
83
		$command = $this->db->createCommand($sql);
Qiang Xue committed
84
		foreach ($messages as $message) {
w  
Qiang Xue committed
85 86 87 88 89 90
			$command->bindValues(array(
				':level' => $message[1],
				':category' => $message[2],
				':log_time' => $message[3],
				':message' => $message[0],
			))->execute();
w  
Qiang Xue committed
91 92 93
		}
	}
}