|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Profiler; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Strings\WildcardPattern; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Target is the base class for all profiling target classes. |
|
11
|
|
|
* |
|
12
|
|
|
* A profile target object will filter the messages stored by {@see Profiler} according |
|
13
|
|
|
* to its {@see include} and {@see exclude} properties. |
|
14
|
|
|
* |
|
15
|
|
|
* For more details and usage information on Target, |
|
16
|
|
|
* see the [guide article on profiling & targets](guide:runtime-profiling). |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class Target |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array list of message categories that this target is interested in. Defaults to empty, meaning all |
|
22
|
|
|
* categories. |
|
23
|
|
|
* |
|
24
|
|
|
* You can use an asterisk at the end of a category so that the category may be used to |
|
25
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match |
|
26
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
|
27
|
|
|
*/ |
|
28
|
|
|
private array $include = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no |
|
32
|
|
|
* uninteresting messages. |
|
33
|
|
|
* |
|
34
|
|
|
* If this property is not empty, then any category listed here will be excluded from {@see include}. |
|
35
|
|
|
* You can use an asterisk at the end of a category so that the category can be used to |
|
36
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match |
|
37
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
|
38
|
|
|
* |
|
39
|
|
|
* {@see include} |
|
40
|
|
|
*/ |
|
41
|
|
|
private array $exclude = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool whether to enable this log target. Defaults to true. |
|
45
|
|
|
*/ |
|
46
|
|
|
private bool $enabled = true; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Processes the given log messages. |
|
50
|
|
|
* |
|
51
|
|
|
* This method will filter the given messages with {@see levels} and {@see categories}. |
|
52
|
|
|
* And if requested, it will also export the filtering result to specific medium (e.g. email). |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $messages profiling messages to be processed. See {@see Profiler::$messages} for the structure |
|
55
|
|
|
* of each message. |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function collect(array $messages): void |
|
58
|
|
|
{ |
|
59
|
5 |
|
if (!$this->enabled) { |
|
60
|
1 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
$messages = $this->filterMessages($messages); |
|
64
|
|
|
|
|
65
|
4 |
|
if (count($messages) > 0) { |
|
66
|
3 |
|
$this->export($messages); |
|
67
|
|
|
} |
|
68
|
4 |
|
} |
|
69
|
|
|
|
|
70
|
5 |
|
public function include(array $include): self |
|
71
|
|
|
{ |
|
72
|
5 |
|
$this->include = $include; |
|
73
|
5 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
public function exclude(array $exclude): self |
|
77
|
|
|
{ |
|
78
|
6 |
|
$this->exclude = $exclude; |
|
79
|
6 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function enable(): self |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->enabled = true; |
|
85
|
1 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function disable(): self |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->enabled = false; |
|
91
|
1 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
public function isEnabled(): bool |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->enabled; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Exports profiling messages to a specific destination. |
|
101
|
|
|
* |
|
102
|
|
|
* Child classes must implement this method. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Message[] $messages profiling messages to be exported. |
|
105
|
|
|
*/ |
|
106
|
|
|
abstract public function export(array $messages); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Filters the given messages according to their categories. |
|
110
|
|
|
* |
|
111
|
|
|
* @param Message[] $messages messages to be filtered. |
|
112
|
|
|
* The message structure follows that in {@see Profiler::$messages}. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array the filtered messages. |
|
115
|
|
|
*/ |
|
116
|
9 |
|
protected function filterMessages(array $messages): array |
|
117
|
|
|
{ |
|
118
|
9 |
|
foreach ($messages as $i => $message) { |
|
119
|
9 |
|
if (!$this->isCategoryMatched($message->level())) { |
|
120
|
3 |
|
unset($messages[$i]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
9 |
|
return $messages; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
9 |
|
private function isCategoryMatched($category): bool |
|
127
|
|
|
{ |
|
128
|
9 |
|
$matched = empty($this->include); |
|
129
|
|
|
|
|
130
|
9 |
|
foreach ($this->include as $pattern) { |
|
131
|
2 |
|
if ((new WildcardPattern($pattern))->match($category)) { |
|
132
|
1 |
|
$matched = true; |
|
133
|
1 |
|
break; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
9 |
|
if ($matched) { |
|
138
|
8 |
|
foreach ($this->exclude as $pattern) { |
|
139
|
3 |
|
if ((new WildcardPattern($pattern))->match($category)) { |
|
140
|
2 |
|
$matched = false; |
|
141
|
2 |
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
9 |
|
return $matched; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|