DbTarget.php 2.56 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;
14
use yii\helpers\VarDumper;
Qiang Xue committed
15

w  
Qiang Xue committed
16
/**
w  
Qiang Xue committed
17
 * DbTarget stores log messages in a database table.
w  
Qiang Xue committed
18
 *
19 20 21 22 23 24 25 26 27
 * The database connection is specified by [[db]]. Database schema could be initialized by applying migration:
 *
 * ```
 * yii migrate --migrationPath=@yii/log/migrations/
 * ```
 *
 * If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.
 *
 * You may change the name of the table used to store the data by setting [[logTable]].
w  
Qiang Xue committed
28 29
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
30
 * @since 2.0
w  
Qiang Xue committed
31
 */
w  
Qiang Xue committed
32
class DbTarget extends Target
w  
Qiang Xue committed
33
{
34
    /**
35
     * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
36 37
     * After the DbTarget object is created, if you want to change this property, you should only assign it
     * with a DB connection object.
38
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
39 40 41
     */
    public $db = 'db';
    /**
42
     * @var string name of the DB table to store cache content. Defaults to "log".
43 44
     */
    public $logTable = '{{%log}}';
w  
Qiang Xue committed
45

46

47 48 49 50 51 52 53 54
    /**
     * 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();
55
        $this->db = Instance::ensure($this->db, Connection::className());
56
    }
Qiang Xue committed
57

58 59 60 61 62 63
    /**
     * Stores log messages to DB.
     */
    public function export()
    {
        $tableName = $this->db->quoteTableName($this->logTable);
64 65
        $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]])
                VALUES (:level, :category, :log_time, :prefix, :message)";
66 67
        $command = $this->db->createCommand($sql);
        foreach ($this->messages as $message) {
68 69
            list($text, $level, $category, $timestamp) = $message;
            if (!is_string($text)) {
70
                $text = VarDumper::export($text);
71
            }
72
            $command->bindValues([
73 74 75 76 77
                ':level' => $level,
                ':category' => $category,
                ':log_time' => $timestamp,
                ':prefix' => $this->getMessagePrefix($message),
                ':message' => $text,
78 79 80
            ])->execute();
        }
    }
w  
Qiang Xue committed
81
}