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