1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\log; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\db\Connection; |
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
use yii\di\Instance; |
14
|
|
|
use yii\helpers\VarDumper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* DbTarget stores log messages in a database table. |
18
|
|
|
* |
19
|
|
|
* The database connection is specified by [[db]]. Database schema could be initialized by applying migration: |
20
|
|
|
* |
21
|
|
|
* ``` |
22
|
|
|
* yii migrate --migrationPath=@yii/log/migrations/ |
23
|
|
|
* ``` |
24
|
|
|
* |
25
|
|
|
* If you don't want to use migration and need SQL instead, files for all databases are in migrations directory. |
26
|
|
|
* |
27
|
|
|
* You may change the name of the table used to store the data by setting [[logTable]]. |
28
|
|
|
* |
29
|
|
|
* @author Qiang Xue <[email protected]> |
30
|
|
|
* @since 2.0 |
31
|
|
|
*/ |
32
|
|
|
class DbTarget extends Target |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
36
|
|
|
* After the DbTarget object is created, if you want to change this property, you should only assign it |
37
|
|
|
* 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
|
|
|
public $db = 'db'; |
41
|
|
|
/** |
42
|
|
|
* @var string name of the DB table to store cache content. Defaults to "log". |
43
|
|
|
*/ |
44
|
|
|
public $logTable = '{{%log}}'; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the DbTarget component. |
49
|
|
|
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
50
|
|
|
* @throws InvalidConfigException if [[db]] is invalid. |
51
|
|
|
*/ |
52
|
6 |
|
public function init() |
53
|
|
|
{ |
54
|
6 |
|
parent::init(); |
55
|
6 |
|
$this->db = Instance::ensure($this->db, Connection::className()); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Stores log messages to DB. |
60
|
|
|
*/ |
61
|
6 |
|
public function export() |
62
|
|
|
{ |
63
|
6 |
|
if ($this->db->getTransaction()) { |
64
|
|
|
// create new database connection, if there is an open transaction |
65
|
|
|
// to ensure insert statement is not affected by a rollback |
66
|
3 |
|
$this->db = clone $this->db; |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
$tableName = $this->db->quoteTableName($this->logTable); |
70
|
6 |
|
$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]]) |
71
|
|
|
VALUES (:level, :category, :log_time, :prefix, :message)"; |
72
|
6 |
|
$command = $this->db->createCommand($sql); |
73
|
6 |
|
foreach ($this->messages as $message) { |
74
|
6 |
|
list($text, $level, $category, $timestamp) = $message; |
75
|
6 |
|
if (!is_string($text)) { |
76
|
|
|
// exceptions may not be serializable if in the call stack somewhere is a Closure |
77
|
|
|
if ($text instanceof \Throwable || $text instanceof \Exception) { |
78
|
|
|
$text = (string) $text; |
79
|
|
|
} else { |
80
|
|
|
$text = VarDumper::export($text); |
81
|
|
|
} |
82
|
|
|
} |
83
|
6 |
|
$command->bindValues([ |
84
|
6 |
|
':level' => $level, |
85
|
6 |
|
':category' => $category, |
86
|
6 |
|
':log_time' => $timestamp, |
87
|
6 |
|
':prefix' => $this->getMessagePrefix($message), |
88
|
6 |
|
':message' => $text, |
89
|
6 |
|
])->execute(); |
90
|
|
|
} |
91
|
6 |
|
} |
92
|
|
|
} |
93
|
|
|
|