1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Logger |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Queue\PriorityQueue; |
18
|
|
|
use Phossa2\Shared\Globbing\GlobbingTrait; |
19
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
20
|
|
|
use Phossa2\Shared\Queue\PriorityQueueInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ExtendedLoggerTrait |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Logger |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see interface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait ExtendedLoggerTrait |
32
|
|
|
{ |
33
|
|
|
use GlobbingTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Collection of handler queues |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $handlers = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Collection of processor queues |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $processors = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* default channel name |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $default_channel; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Current channel name |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
* @access protected |
64
|
|
|
*/ |
65
|
|
|
protected $current_channel; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get current channel, if not set, get the default channel |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* @access protected |
72
|
|
|
*/ |
73
|
|
|
protected function getChannel()/*# : string */ |
74
|
|
|
{ |
75
|
|
|
return $this->current_channel ?: $this->default_channel; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add callable to the $type queue |
80
|
|
|
* |
81
|
|
|
* @param string $type 'handlers' or 'processors' |
82
|
|
|
* @param callable $callable |
83
|
|
|
* @param string $channel |
84
|
|
|
* @param int $priority -100 - +100 |
85
|
|
|
* @return $this |
86
|
|
|
* @access protected |
87
|
|
|
*/ |
88
|
|
|
protected function addCallable( |
89
|
|
|
/*# string */ $type, |
90
|
|
|
callable $callable, |
91
|
|
|
/*# string */ $channel, |
92
|
|
|
/*# int */ $priority |
93
|
|
|
) { |
94
|
|
|
$q = &$this->$type; |
95
|
|
|
$c = strtoupper($channel); |
96
|
|
|
|
97
|
|
|
if (!isset($q[$c])) { |
98
|
|
|
$q[$c] = new PriorityQueue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* @var PriorityQueue $queue */ |
102
|
|
|
$queue = $q[$c]; |
103
|
|
|
$queue->insert($callable, $priority); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove callable for $type |
110
|
|
|
* |
111
|
|
|
* @param string $type |
112
|
|
|
* @param callable|string $callableOrClassname |
113
|
|
|
* @param string $channel |
114
|
|
|
* @return $this |
115
|
|
|
* @access protected |
116
|
|
|
*/ |
117
|
|
|
protected function removeCallable( |
118
|
|
|
/*# string */ $type, |
119
|
|
|
$callableOrClassname, |
120
|
|
|
/*# string */ $channel |
121
|
|
|
) { |
122
|
|
|
$channels = $channel ? (array) $channel : $this->getAllChannels($type); |
123
|
|
|
|
124
|
|
|
$q = &$this->$type; |
125
|
|
|
foreach ($channels as $ch) { |
126
|
|
|
$c = strtoupper($ch); |
127
|
|
|
/* @var PriorityQueue $queue */ |
128
|
|
|
if (isset($q[$c])) { |
129
|
|
|
$this->removeFromQueue($q[$c], $callableOrClassname); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Remove callable or matching classname object from the queue |
137
|
|
|
* |
138
|
|
|
* @param PriorityQueueInterface $queue |
139
|
|
|
* @param callable|string $callabOrClassname |
140
|
|
|
* @access protected |
141
|
|
|
*/ |
142
|
|
|
protected function removeFromQueue( |
143
|
|
|
PriorityQueueInterface $queue, |
144
|
|
|
$callabOrClassname |
145
|
|
|
) { |
146
|
|
|
if (is_object($callabOrClassname)) { |
147
|
|
|
$queue->remove($callabOrClassname); |
148
|
|
|
} else { |
149
|
|
|
foreach ($queue as $data) { |
150
|
|
|
if (is_a($data['data'], $callabOrClassname)) { |
151
|
|
|
$queue->remove($data['data']); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get all the channels for handlers or processors |
159
|
|
|
* |
160
|
|
|
* @param string $type 'handlers' or 'processors' |
161
|
|
|
* @return array |
162
|
|
|
* @access protected |
163
|
|
|
*/ |
164
|
|
|
protected function getAllChannels(/*# string */ $type)/*# : array */ |
165
|
|
|
{ |
166
|
|
|
return array_keys($this->$type); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Execute related processors on the log entry |
171
|
|
|
* |
172
|
|
|
* @param LogEntryInterface $logEntry |
173
|
|
|
* @return $this |
174
|
|
|
* @access protected |
175
|
|
|
*/ |
176
|
|
|
protected function runProcessors(LogEntryInterface $logEntry) |
177
|
|
|
{ |
178
|
|
|
// get related processors |
179
|
|
|
$queue = $this->getCallables('processors', $logEntry->getChannel()); |
180
|
|
|
|
181
|
|
|
// loop thru these processors |
182
|
|
|
foreach($queue as $data) { |
183
|
|
|
$processor = $data['data']; |
184
|
|
|
$processor($logEntry); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Execute related handlers on the log entry |
192
|
|
|
* |
193
|
|
|
* @param LogEntryInterface $logEntry |
194
|
|
|
* @return $this |
195
|
|
|
* @access protected |
196
|
|
|
*/ |
197
|
|
|
protected function runHandlers(LogEntryInterface $logEntry) |
198
|
|
|
{ |
199
|
|
|
// get related handlers |
200
|
|
|
$queue = $this->getCallables('handlers', $logEntry->getChannel()); |
201
|
|
|
|
202
|
|
|
// loop thru these handlers |
203
|
|
|
foreach($queue as $data) { |
204
|
|
|
// stopped ? |
205
|
|
|
if ($logEntry->isPropagationStopped()) { |
206
|
|
|
break; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// run handler |
210
|
|
|
$handler = $data['data']; |
211
|
|
|
$handler($logEntry); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get all matching handlers/processors with channel name globbing |
219
|
|
|
* |
220
|
|
|
* @param string $type |
221
|
|
|
* @param string $channel |
222
|
|
|
* @return PriorityQueue |
223
|
|
|
* @access protected |
224
|
|
|
*/ |
225
|
|
|
protected function getCallables( |
226
|
|
|
/*# string */ $type, |
227
|
|
|
/*# string */ $channel |
228
|
|
|
)/*# : PriorityQueue */ { |
229
|
|
|
|
230
|
|
|
// name globbing with all channels |
231
|
|
|
$matchedChannels = $this->globbingNames( |
232
|
|
|
$channel, $this->getAllChannels($type) |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
// type queues |
236
|
|
|
$q = &$this->$type; |
237
|
|
|
|
238
|
|
|
// merge queues |
239
|
|
|
$queue = new PriorityQueue(); |
240
|
|
|
foreach ($matchedChannels as $c) { |
241
|
|
|
$queue = $queue->combine($q[$c]); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $queue; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|