DbTarget.php 3.31 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * DbTarget class file.
w  
Qiang Xue committed
4 5 6
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
7
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
8 9 10
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11
namespace yii\logging;
w  
Qiang Xue committed
12 13

/**
w  
Qiang Xue committed
14
 * DbTarget stores log messages in a database table.
w  
Qiang Xue committed
15
 *
w  
Qiang Xue committed
16 17 18 19
 * 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.
w  
Qiang Xue committed
20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
22
 * @since 2.0
w  
Qiang Xue committed
23
 */
w  
Qiang Xue committed
24
class DbTarget extends Target
w  
Qiang Xue committed
25 26
{
	/**
w  
Qiang Xue committed
27 28 29 30
	 * @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
w  
Qiang Xue committed
31
	 */
w  
Qiang Xue committed
32
	public $connectionID = 'db';
w  
Qiang Xue committed
33
	/**
w  
Qiang Xue committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	 * @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.
w  
Qiang Xue committed
59
	 */
w  
Qiang Xue committed
60
	public $tableName = '{{log}}';
w  
Qiang Xue committed
61

w  
Qiang Xue committed
62
	private $_db;
w  
Qiang Xue committed
63 64

	/**
w  
Qiang Xue committed
65 66 67
	 * 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.
w  
Qiang Xue committed
68
	 */
w  
Qiang Xue committed
69
	public function getDbConnection()
w  
Qiang Xue committed
70
	{
w  
Qiang Xue committed
71
		if ($this->_db !== null) {
w  
Qiang Xue committed
72 73
			return $this->_db;
		}
Qiang Xue committed
74
		$this->_db = \Yii::$application->getComponent($this->connectionID);
w  
Qiang Xue committed
75 76
		if (!$this->_db instanceof \yii\db\dao\Connection) {
			throw new \yii\base\Exception('DbTarget.connectionID must refer to a valid application component ID');
w  
Qiang Xue committed
77 78 79 80
		}
	}

	/**
w  
Qiang Xue committed
81 82
	 * Stores log [[messages]] to DB.
	 * @param boolean $final whether this method is called at the end of the current application
w  
Qiang Xue committed
83
	 */
w  
Qiang Xue committed
84
	public function exportMessages($final)
w  
Qiang Xue committed
85
	{
w  
Qiang Xue committed
86 87 88
		$sql = "INSERT INTO {$this->tableName}
			(level, category, log_time, message) VALUES
			(:level, :category, :log_time, :message)";
w  
Qiang Xue committed
89
		$command = $this->getDbConnection()->createCommand($sql);
w  
Qiang Xue committed
90 91 92 93 94 95 96
		foreach ($this->messages as $message) {
			$command->bindValues(array(
				':level' => $message[1],
				':category' => $message[2],
				':log_time' => $message[3],
				':message' => $message[0],
			))->execute();
w  
Qiang Xue committed
97
		}
w  
Qiang Xue committed
98 99

		$this->messages = array();
w  
Qiang Xue committed
100 101
	}
}