-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDbTarget.php
110 lines (96 loc) · 3.83 KB
/
DbTarget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace apollo11\logger;
use yii\base\Exception;
use yii\console\Request;
use yii\db\Connection;
use yii\di\Instance;
use yii\helpers\VarDumper;
class DbTarget extends Target
{
/**
* @var Connection|array|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. Defaults to "log".
*/
public $logTable = '{{%apollo11_sys_log}}';
/**
* Initializes the DbTarget component.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::class);
}
public function sendMessage()
{
if ($this->db->getTransaction()) {
$this->db = clone $this->db;
}
if (\Yii::$app->db->schema->getTableSchema($this->logTable) !== null) {
$tableName = $this->db->quoteTableName($this->logTable);
$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]],[[text]],[[user_agent]],[[remote_ip]])
VALUES (:level, :category, :log_time, :prefix, :message,:text,:user_agent,:remote_ip)";
$command = $this->db->createCommand($sql);
foreach ($this->config['messages'] as $message) {
if ($command->bindValues([
':level' => $message['level'],
':category' => $message['category'],
':log_time' => $message['timestamp'],
':prefix' => $message['prefix'],
':message' => $message['text'],
':text' => $message['formattedMessage'],
':user_agent' => $this->config['user_agent'],
':remote_ip' => $this->config['remote_ip'],
])->execute() > 0) {
continue;
}
throw new Exception('Unable to export log through database!');
}
} else {
throw new Exception('Table ' . $this->logTable . ' Does not exist');
}
}
protected function prepareConfig()
{
$messages = [];
if ($this->getLevels()>>2 === 0) array_pop($this->messages);
foreach ($this->messages as $key => $message) {
list($text, $level, $category, $timestamp) = $message;
if (!is_string($text)) {
if ($text instanceof \Throwable || $text instanceof \Exception) {
$text = (string)$text;
} else {
$text = VarDumper::export($text);
}
}
$messages[$key]['text'] = $text;
$messages[$key]['level'] = $level;
$messages[$key]['category'] = $category;
$messages[$key]['formattedMessage'] = $text.PHP_EOL.$this->getContextMessage();
$messages[$key]['timestamp'] = $timestamp;
$messages[$key]['prefix'] = $this->getMessagePrefix($message);
}
$userAgent = null;
if (!(\Yii::$app->request instanceof Request)){
$userAgent = \Yii::$app->request->getUserAgent();
}
$this->config = [
'messages' => $messages,
'user_agent' => $userAgent,
'remote_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null
];
}
public function getTableName(){
return $this->logTable;
}
}