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\Logger\Message\Message; |
18
|
|
|
use Phossa2\Shared\Queue\PriorityQueue; |
19
|
|
|
use Phossa2\Shared\Globbing\GlobbingTrait; |
20
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
21
|
|
|
use Phossa2\Logger\Exception\RuntimeException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ExtendedLoggerTrait |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Logger |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see interface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
trait ExtendedLoggerTrait |
33
|
|
|
{ |
34
|
|
|
use GlobbingTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Collection of handler queues |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $handlers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Collection of processor queues |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $processors = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* channel name |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected $channel = 'LOGGER'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* marker for set channel |
62
|
|
|
* |
63
|
|
|
* @var bool |
64
|
|
|
* @access protected |
65
|
|
|
*/ |
66
|
|
|
protected $channel_set = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set channel for this log |
70
|
|
|
* |
71
|
|
|
* @param string $channel |
72
|
|
|
* @return $this |
73
|
|
|
* @access protected |
74
|
|
|
*/ |
75
|
|
|
protected function setChannel(/*# string */ $channel) |
76
|
|
|
{ |
77
|
|
|
$this->channel_set = true; |
78
|
|
|
$this->channel = strtoupper($channel); |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get logger channel |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* @access protected |
87
|
|
|
*/ |
88
|
|
|
protected function getChannel()/*# : string */ |
89
|
|
|
{ |
90
|
|
|
return $this->channel; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add callable to the $type queue |
95
|
|
|
* |
96
|
|
|
* @param string $type 'handlers' or 'processors' |
97
|
|
|
* @param callable $callable |
98
|
|
|
* @param string $channel |
99
|
|
|
* @param int $priority -100 - +100 |
100
|
|
|
* @return $this |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
|
|
protected function addCallable( |
104
|
|
|
/*# string */ $type, |
105
|
|
|
callable $callable, |
106
|
|
|
/*# string */ $channel, |
107
|
|
|
/*# int */ $priority |
108
|
|
|
) { |
109
|
|
|
// use current $logger channel |
110
|
|
|
if (empty($channel)) { |
111
|
|
|
$channel = $this->getChannel(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$q = &$this->$type; |
115
|
|
|
if (!isset($q[$channel])) { |
116
|
|
|
$q[$channel] = new PriorityQueue(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* @var PriorityQueue $queue */ |
120
|
|
|
$queue = $q[$channel]; |
121
|
|
|
$queue->insert($callable, $priority); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Remove callable for $type |
128
|
|
|
* |
129
|
|
|
* @param string $type |
130
|
|
|
* @param callable $callable |
131
|
|
|
* @param string $channel |
132
|
|
|
* @return $this |
133
|
|
|
* @access protected |
134
|
|
|
*/ |
135
|
|
|
protected function removeCallable( |
136
|
|
|
/*# string */ $type, |
137
|
|
|
callable $callable, |
138
|
|
|
/*# string */ $channel |
139
|
|
|
) { |
140
|
|
|
$channels = $channel ? (array) $channel : $this->getAllChannels($type); |
141
|
|
|
|
142
|
|
|
$q = &$this->$type; |
143
|
|
|
foreach ($channels as $c) { |
144
|
|
|
/* @var PriorityQueue $queue */ |
145
|
|
|
if (isset($q[$c])) { |
146
|
|
|
$queue = $q[$c]; |
147
|
|
|
$queue->remove($callable); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get all the channels for handlers or processors |
155
|
|
|
* |
156
|
|
|
* @param string $type 'handlers' or 'processors' |
157
|
|
|
* @return array |
158
|
|
|
* @access protected |
159
|
|
|
*/ |
160
|
|
|
protected function getAllChannels(/*# string */ $type)/*# : array */ |
161
|
|
|
{ |
162
|
|
|
return array_keys($this->$type); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Execute related processors on the log entry |
167
|
|
|
* |
168
|
|
|
* @param LogEntryInterface $logEntry |
169
|
|
|
* @return $this |
170
|
|
|
* @access protected |
171
|
|
|
*/ |
172
|
|
|
protected function runProcessors(LogEntryInterface $logEntry) |
173
|
|
|
{ |
174
|
|
|
// get related processors |
175
|
|
|
$queue = $this->getCallables('processors', $logEntry->getChannel()); |
176
|
|
|
|
177
|
|
|
// loop thru these processors |
178
|
|
|
foreach($queue as $data) { |
179
|
|
|
$processor = $data['data']; |
180
|
|
|
$processor($logEntry); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Execute related handlers on the log entry |
188
|
|
|
* |
189
|
|
|
* @param LogEntryInterface $logEntry |
190
|
|
|
* @return $this |
191
|
|
|
* @access protected |
192
|
|
|
*/ |
193
|
|
|
protected function runHandlers(LogEntryInterface $logEntry) |
194
|
|
|
{ |
195
|
|
|
// get related handlers |
196
|
|
|
$queue = $this->getCallables('handlers', $logEntry->getChannel()); |
197
|
|
|
|
198
|
|
|
// loop thru these handlers |
199
|
|
|
foreach($queue as $data) { |
200
|
|
|
if ($logEntry->isPropagationStopped()) { |
201
|
|
|
break; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$handler = $data['data']; |
205
|
|
|
|
206
|
|
|
if ($this->isValidHandler($logEntry, $handler)) { |
207
|
|
|
$handler($logEntry); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get all matching handlers/processors with channel name globbing |
216
|
|
|
* |
217
|
|
|
* @param string $channel |
218
|
|
|
* @return PriorityQueue |
219
|
|
|
* @access protected |
220
|
|
|
*/ |
221
|
|
|
protected function getCallables( |
222
|
|
|
/*# string */ $type, |
223
|
|
|
/*# string */ $channel |
224
|
|
|
)/*# : PriorityQueue */ { |
225
|
|
|
|
226
|
|
|
// name globbing with all channels |
227
|
|
|
$matchedChannels = $this->globbingNames( |
228
|
|
|
$channel, $this->getAllChannels($type) |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
// type queues |
232
|
|
|
$q = &$this->$type; |
233
|
|
|
|
234
|
|
|
// merge queues |
235
|
|
|
$queue = new PriorityQueue(); |
236
|
|
|
foreach ($matchedChannels as $c) { |
237
|
|
|
$queue = $queue->combine($q[$c]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $queue; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Is this handler can do this log ? |
245
|
|
|
* |
246
|
|
|
* @param LogEntryInterface $logEntry |
247
|
|
|
* @param callable $handler |
248
|
|
|
* @return bool |
249
|
|
|
* @access protected |
250
|
|
|
*/ |
251
|
|
|
protected function isValidHandler( |
252
|
|
|
LogEntryInterface $logEntry, |
253
|
|
|
callable $handler |
254
|
|
|
)/*# : bool */ { |
255
|
|
|
if (is_a($handler, 'Phossa2\\Logger\\Handler\\HandlerInterface')) { |
256
|
|
|
return $handler->isHandling($logEntry->getLevel()); |
|
|
|
|
257
|
|
|
} else { |
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Check if channel is set with $logger($channel) |
264
|
|
|
* |
265
|
|
|
* @throws RuntimeException |
266
|
|
|
* @access protected |
267
|
|
|
*/ |
268
|
|
|
protected function isChannelSet() |
269
|
|
|
{ |
270
|
|
|
if (!$this->channel_set) { |
271
|
|
|
throw new RuntimeException( |
272
|
|
|
Message::get(Message::LOG_CHANNEL_NOTSET), |
273
|
|
|
Message::LOG_CHANNEL_NOTSET |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Mark channel is unset |
280
|
|
|
* |
281
|
|
|
* @access protected |
282
|
|
|
*/ |
283
|
|
|
protected function unsetChannel() |
284
|
|
|
{ |
285
|
|
|
$this->channel_set = false; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.