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

w  
Qiang Xue committed
10
namespace yii\logging;
w  
Qiang Xue committed
11

Qiang Xue committed
12 13 14
use yii\db\Connection;
use yii\base\InvalidConfigException;

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16
 * DbTarget stores log messages in a database table.
w  
Qiang Xue committed
17
 *
w  
Qiang Xue committed
18 19 20 21
 * 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
22 23
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
24
 * @since 2.0
w  
Qiang Xue committed
25
 */
w  
Qiang Xue committed
26
class DbTarget extends Target
w  
Qiang Xue committed
27 28
{
	/**
Qiang Xue committed
29
	 * @var string the ID of [[Connection]] application component.
w  
Qiang Xue committed
30 31 32
	 * 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
33
	 */
w  
Qiang Xue committed
34
	public $connectionID = 'db';
w  
Qiang Xue committed
35
	/**
Qiang Xue committed
36
	 * @var string the name of the DB table that stores log messages. Defaults to 'tbl_log'.
w  
Qiang Xue committed
37
	 *
Qiang Xue committed
38
	 * The DB table should have the following structure:
w  
Qiang Xue committed
39 40 41 42
	 *
	 * ~~~
	 * CREATE TABLE tbl_log (
	 *	   id       INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
Qiang Xue committed
43
	 *	   level    INTEGER,
w  
Qiang Xue committed
44 45 46 47 48 49 50 51 52 53
	 *	   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
Qiang Xue committed
54
	 * to adjust it accordingly. For example, in PostgreSQL, it should be `id SERIAL PRIMARY KEY`.
w  
Qiang Xue committed
55 56 57
	 *
	 * 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
Qiang Xue committed
58
	 * want to create additional indexes (e.g. index on log_time).
w  
Qiang Xue committed
59
	 */
Qiang Xue committed
60
	public $tableName = 'tbl_log';
w  
Qiang Xue committed
61

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

	/**
w  
Qiang Xue committed
65
	 * Returns the DB connection used for saving log messages.
Qiang Xue committed
66 67
	 * @return Connection the DB connection instance
	 * @throws InvalidConfigException if [[connectionID]] does not point to a valid application component.
w  
Qiang Xue committed
68
	 */
Qiang Xue committed
69
	public function getDb()
w  
Qiang Xue committed
70
	{
71
		if ($this->_db === null) {
Qiang Xue committed
72
			$db = \Yii::$app->getComponent($this->connectionID);
Qiang Xue committed
73 74 75 76
			if ($db instanceof Connection) {
				$this->_db = $db;
			} else {
				throw new InvalidConfigException("DbTarget::connectionID must refer to the ID of a DB application component.");
77
			}
w  
Qiang Xue committed
78
		}
79
		return $this->_db;
w  
Qiang Xue committed
80 81
	}

Qiang Xue committed
82 83 84 85 86 87 88 89 90
	/**
	 * Sets the DB connection used by the cache component.
	 * @param Connection $value the DB connection instance
	 */
	public function setDb($value)
	{
		$this->_db = $value;
	}

w  
Qiang Xue committed
91
	/**
Qiang Xue committed
92 93 94
	 * 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
95
	 */
Qiang Xue committed
96
	public function export($messages)
w  
Qiang Xue committed
97
	{
Qiang Xue committed
98 99 100 101
		$db = $this->getDb();
		$tableName = $db->quoteTableName($this->tableName);
		$sql = "INSERT INTO $tableName (level, category, log_time, message) VALUES (:level, :category, :log_time, :message)";
		$command = $db->createCommand($sql);
Qiang Xue committed
102
		foreach ($messages as $message) {
w  
Qiang Xue committed
103 104 105 106 107 108
			$command->bindValues(array(
				':level' => $message[1],
				':category' => $message[2],
				':log_time' => $message[3],
				':message' => $message[0],
			))->execute();
w  
Qiang Xue committed
109 110 111
		}
	}
}