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\base\InvalidConfigException; |
12
|
|
|
use yii\db\Connection; |
13
|
|
|
use yii\di\Instance; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* DbTarget stores log messages in a database table. |
17
|
|
|
* |
18
|
|
|
* The database connection is specified by [[db]]. Database schema could be initialized by applying migration: |
19
|
|
|
* |
20
|
|
|
* ``` |
21
|
|
|
* yii migrate --migrationPath=@yii/log/migrations/ |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* If you don't want to use migration and need SQL instead, files for all databases are in migrations directory. |
25
|
|
|
* |
26
|
|
|
* You may change the name of the table used to store the data by setting [[logTable]]. |
27
|
|
|
* |
28
|
|
|
* @author Qiang Xue <[email protected]> |
29
|
|
|
* @since 2.0 |
30
|
|
|
*/ |
31
|
|
|
class DbTarget extends Target |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
35
|
|
|
* After the DbTarget object is created, if you want to change this property, you should only assign it |
36
|
|
|
* with a DB connection object. |
37
|
|
|
* Starting from version 2.0.2, this can also be a configuration array for creating the object. |
38
|
|
|
*/ |
39
|
|
|
public $db = 'db'; |
40
|
|
|
/** |
41
|
|
|
* @var string name of the DB table to store cache content. Defaults to "log". |
42
|
|
|
*/ |
43
|
|
|
public $logTable = '{{%log}}'; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializes the DbTarget component. |
48
|
|
|
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
49
|
|
|
* @throws InvalidConfigException if [[db]] is invalid. |
50
|
|
|
*/ |
51
|
6 |
|
public function init() |
52
|
|
|
{ |
53
|
6 |
|
parent::init(); |
54
|
6 |
|
$this->db = Instance::ensure($this->db, Connection::class); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Stores log messages to DB. |
59
|
|
|
*/ |
60
|
6 |
|
public function export() |
61
|
|
|
{ |
62
|
6 |
|
if ($this->db->getTransaction()) { |
63
|
|
|
// create new database connection, if there is an open transaction |
64
|
|
|
// to ensure insert statement is not affected by a rollback |
65
|
3 |
|
$this->db = clone $this->db; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$tableName = $this->db->quoteTableName($this->logTable); |
69
|
6 |
|
$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]]) |
70
|
|
|
VALUES (:level, :category, :log_time, :prefix, :message)"; |
71
|
6 |
|
$command = $this->db->createCommand($sql); |
72
|
6 |
|
foreach ($this->messages as $message) { |
73
|
6 |
|
[$level, $text, $context] = $message; |
|
|
|
|
74
|
6 |
|
$command->bindValues([ |
75
|
6 |
|
':level' => $level, |
76
|
6 |
|
':category' => $context['category'], |
77
|
6 |
|
':log_time' => $context['time'], |
78
|
6 |
|
':prefix' => $this->getMessagePrefix($message), |
79
|
6 |
|
':message' => $text, |
80
|
6 |
|
])->execute(); |
81
|
|
|
} |
82
|
6 |
|
} |
83
|
|
|
} |
84
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.