-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEloquentSQLMessage.php
More file actions
140 lines (115 loc) · 4.03 KB
/
EloquentSQLMessage.php
File metadata and controls
140 lines (115 loc) · 4.03 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace WebmanTech\Logger\Message;
use Closure;
use Illuminate\Database\Connection;
use Illuminate\Database\Events\QueryExecuted;
use WebmanTech\Logger\Helper\StringHelper;
/**
* Eloquent SQL 日志
*/
class EloquentSQLMessage extends BaseMessage
{
use TimeBasedMessageTrait;
protected string $channel = 'sql';
protected array $ignoreSql = [
'select 1', // 心跳 SQL
]; // 忽略的 sql
protected array $ignoreSqlPattern = []; // 正则忽略的 sql
protected bool $logNotSelect = true; // 记录所有非 select 语句
protected ?Closure $checkIsSqlNotSelect = null; // 判断 SQL 是否不是 select
protected bool $bindSQLBindings = true; // 是否绑定 SQL 参数
protected bool $showConnectionName = false; // 是否显示连接名称
protected int $logMaxLength = 1000; // 日志 SQL 长度限制
/** @phpstan-ignore-next-line */
protected ?Closure $extraInfo = null; // 其他信息
final public function appendIgnoreSql(string|array $sql): static
{
$this->ignoreSql = array_unique(array_merge($this->ignoreSql, (array)$sql));
return $this;
}
final public function appendIgnoreSqlPattern(string|array $pattern): static
{
$this->ignoreSqlPattern = array_unique(array_merge($this->ignoreSqlPattern, (array)$pattern));
return $this;
}
/**
* 绑定一个连接
*/
public function bindConnection(Connection $connection): void
{
if (!$this->isEnabled()) {
return;
}
$connection->listen($this->handle(...));
}
/**
* 处理一个 SQL 事件
*/
public function handle(QueryExecuted $event): void
{
if (!$this->isEnabled()) {
return;
}
$sql = $event->sql;
$cost = intval(round($event->time));
// 检查是否需要记录
if (in_array($sql, $this->ignoreSql, true)) {
return;
}
foreach ($this->ignoreSqlPattern as $pattern) {
if (preg_match($pattern, $sql)) {
return;
}
}
// 检查 logLevel 是否需要记录
$logLevel = $this->getLogLevelByTime($cost);
if ($logLevel === null) {
if (!($this->logNotSelect && $this->isSqlNotSelect($sql, $event))) {
return;
}
$logLevel = 'info';
}
// sql 绑定参数
$sql = $this->getBindSQL($event);
$context = [
'cost' => $cost,
];
if ($this->showConnectionName) {
$context['connectionName'] = $event->connectionName;
}
// 添加其他信息
if ($value = $this->callClosure($this->extraInfo, $event)) {
$context = array_merge($context, (array)$value);
}
$this->log($logLevel, StringHelper::limit($sql, $this->logMaxLength), $context);
}
protected function isSqlNotSelect(string $sql, QueryExecuted $event): bool
{
$value = $this->callClosure($this->checkIsSqlNotSelect, $sql, $event);
if ($value !== null) {
return $value;
}
return !!preg_match("/^\s*(update|delete|insert|replace|create|alter|drop|truncate)\s*/i", $sql);
}
private ?bool $isEventHasToRawSql = null;
protected function getBindSQL(QueryExecuted $event): string
{
if (!$this->bindSQLBindings || !$event->bindings) {
return $event->sql;
}
// 为了更高的性能,缓存这个值,以防止在多次调用时一直检查 exist
if ($this->isEventHasToRawSql === null) {
/** @phpstan-ignore-next-line */
$this->isEventHasToRawSql = method_exists($event, 'toRawSql');
}
if ($this->isEventHasToRawSql) {
return $event->toRawSql();
}
$sql = $event->sql;
foreach ($event->bindings as $v) {
/** @var string $sql */
$sql = preg_replace('/\\?/', "'" . (is_string($v) ? addslashes($v) : $v) . "'", $sql, 1);
}
return $sql;
}
}