DbTarget.php 2.87 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/
 */

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

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

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16
 * DbTarget stores log messages in a database table.
w  
Qiang Xue committed
17
 *
18
 * By default, DbTarget stores the log messages in a DB table named 'log'. This table
19
 * must be pre-created. The table name can be changed by setting [[logTable]].
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 27 28 29 30 31 32 33 34 35 36
    /**
     * @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.
     */
    public $db = 'db';
    /**
     * @var string name of the DB table to store cache content.
     * The table should be pre-created as follows:
     *
     * ~~~
37
     * CREATE TABLE log (
38 39 40 41 42
     *     id       BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     *     level    INTEGER,
     *     category VARCHAR(255),
     *     log_time INTEGER,
     *     message  TEXT,
43 44 45 46 47 48 49 50 51 52 53 54 55 56
     *     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`.
     *
     * 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`).
     */
    public $logTable = '{{%log}}';
w  
Qiang Xue committed
57

58 59 60 61 62 63 64 65
    /**
     * 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.
     */
    public function init()
    {
        parent::init();
66
        $this->db = Instance::ensure($this->db, Connection::className());
67
    }
Qiang Xue committed
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /**
     * Stores log messages to DB.
     */
    public function export()
    {
        $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);
        foreach ($this->messages as $message) {
            $command->bindValues([
                ':level' => $message[1],
                ':category' => $message[2],
                ':log_time' => $message[3],
                ':message' => $message[0],
            ])->execute();
        }
    }
w  
Qiang Xue committed
87
}