DbTarget.php 2.46 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 36 37 38 39 40
    /**
     * @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';
    /**
41
     * @var string name of the DB table to store cache content. Defaults to "log".
42 43
     */
    public $logTable = '{{%log}}';
w  
Qiang Xue committed
44

45

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

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