|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Logger\Logger |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link http://github.com/appserver-io/logger |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Logger; |
|
22
|
|
|
|
|
23
|
|
|
use Psr\Log\LogLevel; |
|
24
|
|
|
use AppserverIo\Logger\Handlers\HandlerInterface; |
|
25
|
|
|
use AppserverIo\Logger\Processors\ProcessorInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Thread-Safe and PSR-3 compatible logger implementation. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link http://github.com/appserver-io/logger |
|
34
|
|
|
* @link http://www.appserver.io |
|
35
|
|
|
*/ |
|
36
|
|
|
class Logger implements ThreadSafeLoggerInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The thread context of the thread we're logging for. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static $threadContext = 'global'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The channel name we log to. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $channelName; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Array with the handlers. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $handlers; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Array with the processors. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $processors; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Initializes the logger instance with the log level. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $channelName The channel name |
|
71
|
|
|
* @param array $handlers The array with the handlers |
|
72
|
|
|
* @param array $processors The array with the processors |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function __construct($channelName, array $handlers = array(), array $processors = array()) |
|
75
|
|
|
{ |
|
76
|
|
|
|
|
77
|
|
|
// initialize the passe values |
|
78
|
2 |
|
$this->channelName = $channelName; |
|
79
|
|
|
|
|
80
|
|
|
// initialize the stackables |
|
81
|
2 |
|
$this->handlers = array(); |
|
82
|
2 |
|
$this->processors = array(); |
|
83
|
|
|
|
|
84
|
|
|
// add the passed handlers |
|
85
|
2 |
|
foreach ($handlers as $handler) { |
|
86
|
|
|
$this->addHandler($handler); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// add the passed processors |
|
90
|
2 |
|
foreach ($processors as $processor) { |
|
91
|
|
|
$this->addProcessor($processor); |
|
92
|
|
|
} |
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Adds the passed handler. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \AppserverIo\Logger\Handlers\HandlerInterface $handler The handler to be added |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function addHandler(HandlerInterface $handler) |
|
103
|
|
|
{ |
|
104
|
2 |
|
$this->handlers[] = $handler; |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the registered handlers. |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Stackable The registered handlers |
|
111
|
|
|
*/ |
|
112
|
2 |
|
protected function getHandlers() |
|
113
|
|
|
{ |
|
114
|
2 |
|
return $this->handlers; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Adds the passed processor. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \AppserverIo\Logger\Processors\ProcessorInterface $processor The processor to be added |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public function addProcessor(ProcessorInterface $processor) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->processors[] = $processor; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the registered processors. |
|
131
|
|
|
* |
|
132
|
|
|
* @return \Stackable The registered processors |
|
133
|
|
|
*/ |
|
134
|
2 |
|
protected function getProcessors() |
|
135
|
|
|
{ |
|
136
|
2 |
|
return $this->processors; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the channel name, passed to the constructor. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string The channel name |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getChannelName() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->channelName; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Adds a log record at the DEBUG level. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $message The log message |
|
153
|
|
|
* @param array $context The log context |
|
154
|
|
|
* |
|
155
|
|
|
* @return boolean Whether the record has been processed |
|
156
|
|
|
*/ |
|
157
|
|
|
public function addDebug($message, array $context = array()) |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->debug($message, $context); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Adds a log record at the INFO level. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $message The log message |
|
166
|
|
|
* @param array $context The log context |
|
167
|
|
|
* |
|
168
|
|
|
* @return boolean Whether the record has been processed |
|
169
|
|
|
*/ |
|
170
|
|
|
public function addInfo($message, array $context = array()) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->info($message, $context); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Adds a log record at the NOTICE level. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $message The log message |
|
179
|
|
|
* @param array $context The log context |
|
180
|
|
|
* |
|
181
|
|
|
* @return boolean Whether the record has been processed |
|
182
|
|
|
*/ |
|
183
|
|
|
public function addNotice($message, array $context = array()) |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->notice($message, $context); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Adds a log record at the WARNING level. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $message The log message |
|
192
|
|
|
* @param array $context The log context |
|
193
|
|
|
* |
|
194
|
|
|
* @return boolean Whether the record has been processed |
|
195
|
|
|
*/ |
|
196
|
|
|
public function addWarning($message, array $context = array()) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->warning($message, $context); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Adds a log record at the ERROR level. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $message The log message |
|
205
|
|
|
* @param array $context The log context |
|
206
|
|
|
* |
|
207
|
|
|
* @return boolean Whether the record has been processed |
|
208
|
|
|
*/ |
|
209
|
|
|
public function addError($message, array $context = array()) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->error($message, $context); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Adds a log record at the CRITICAL level. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $message The log message |
|
218
|
|
|
* @param array $context The log context |
|
219
|
|
|
* |
|
220
|
|
|
* @return boolean Whether the record has been processed |
|
221
|
|
|
*/ |
|
222
|
|
|
public function addCritical($message, array $context = array()) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->critical($message, $context); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Adds a log record at the ALERT level. |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $message The log message |
|
231
|
|
|
* @param array $context The log context |
|
232
|
|
|
* |
|
233
|
|
|
* @return boolean Whether the record has been processed |
|
234
|
|
|
*/ |
|
235
|
|
|
public function addAlert($message, array $context = array()) |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->alert($message, $context); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Adds a log record at the EMERGENCY level. |
|
242
|
|
|
* |
|
243
|
|
|
* @param string $message The log message |
|
244
|
|
|
* @param array $context The log context |
|
245
|
|
|
* |
|
246
|
|
|
* @return boolean Whether the record has been processed |
|
247
|
|
|
*/ |
|
248
|
|
|
public function addEmergency($message, array $context = array()) |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->emergency($message, $context); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* System is unusable. |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $message The message to log |
|
257
|
|
|
* @param array $context The context for log |
|
258
|
|
|
* |
|
259
|
|
|
* @return void |
|
260
|
|
|
*/ |
|
261
|
|
|
public function emergency($message, array $context = array()) |
|
262
|
|
|
{ |
|
263
|
|
|
$this->log(LogLevel::EMERGENCY, $message, $context); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Action must be taken immediately. |
|
268
|
|
|
* |
|
269
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
|
270
|
|
|
* trigger the SMS alerts and wake you up. |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $message The message to log |
|
273
|
|
|
* @param array $context The context for log |
|
274
|
|
|
* |
|
275
|
|
|
* @return void |
|
276
|
|
|
*/ |
|
277
|
|
|
public function alert($message, array $context = array()) |
|
278
|
|
|
{ |
|
279
|
|
|
$this->log(LogLevel::ALERT, $message, $context); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Critical conditions. |
|
284
|
|
|
* |
|
285
|
|
|
* Example: Application component unavailable, unexpected exception. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $message The message to log |
|
288
|
|
|
* @param array $context The context for log |
|
289
|
|
|
* |
|
290
|
|
|
* @return void |
|
291
|
|
|
*/ |
|
292
|
|
|
public function critical($message, array $context = array()) |
|
293
|
|
|
{ |
|
294
|
|
|
$this->log(LogLevel::CRITICAL, $message, $context); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Runtime errors that do not require immediate action but should typically |
|
299
|
|
|
* be logged and monitored. |
|
300
|
|
|
* |
|
301
|
|
|
* @param string $message The message to log |
|
302
|
|
|
* @param array $context The context for log |
|
303
|
|
|
* |
|
304
|
|
|
* @return void |
|
305
|
|
|
*/ |
|
306
|
|
|
public function error($message, array $context = array()) |
|
307
|
|
|
{ |
|
308
|
|
|
$this->log(LogLevel::ERROR, $message, $context); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Exceptional occurrences that are not errors. |
|
313
|
|
|
* |
|
314
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
315
|
|
|
* that are not necessarily wrong. |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $message The message to log |
|
318
|
|
|
* @param array $context The context for log |
|
319
|
|
|
* |
|
320
|
|
|
* @return void |
|
321
|
|
|
*/ |
|
322
|
|
|
public function warning($message, array $context = array()) |
|
323
|
|
|
{ |
|
324
|
|
|
$this->log(LogLevel::WARNING, $message, $context); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Normal but significant events. |
|
329
|
|
|
* |
|
330
|
|
|
* @param string $message The message to log |
|
331
|
|
|
* @param array $context The context for log |
|
332
|
|
|
* |
|
333
|
|
|
* @return null |
|
334
|
|
|
*/ |
|
335
|
|
|
public function notice($message, array $context = array()) |
|
336
|
|
|
{ |
|
337
|
|
|
$this->log(LogLevel::NOTICE, $message, $context); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Interesting events. |
|
342
|
|
|
* |
|
343
|
|
|
* Example: User logs in, SQL logs. |
|
344
|
|
|
* |
|
345
|
|
|
* @param string $message The message to log |
|
346
|
|
|
* @param array $context The context for log |
|
347
|
|
|
* |
|
348
|
|
|
* @return void |
|
349
|
|
|
*/ |
|
350
|
1 |
|
public function info($message, array $context = array()) |
|
351
|
|
|
{ |
|
352
|
1 |
|
$this->log(LogLevel::INFO, $message, $context); |
|
353
|
1 |
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Detailed debug information. |
|
357
|
|
|
* |
|
358
|
|
|
* @param string $message The message to log |
|
359
|
|
|
* @param array $context The context for log |
|
360
|
|
|
* |
|
361
|
|
|
* @return void |
|
362
|
|
|
*/ |
|
363
|
|
|
public function debug($message, array $context = array()) |
|
364
|
|
|
{ |
|
365
|
|
|
$this->log(LogLevel::DEBUG, $message, $context); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Logs with an arbitrary level. |
|
370
|
|
|
* |
|
371
|
|
|
* @param mixed $level The log level |
|
372
|
|
|
* @param string $message The message to log |
|
373
|
|
|
* @param array $context The context for log |
|
374
|
|
|
* |
|
375
|
|
|
* @return void |
|
376
|
|
|
*/ |
|
377
|
2 |
|
public function log($level, $message, array $context = array()) |
|
378
|
|
|
{ |
|
379
|
2 |
|
$this->process(new LogMessage(mt_rand(), $level, $message, $context)); |
|
380
|
2 |
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Targe for the delegate method of the log message that processes |
|
384
|
|
|
* the log message asynchronously. |
|
385
|
|
|
* |
|
386
|
|
|
* @param \AppserverIo\Logger\LogMessageInterface $logMessage The message to be processed |
|
387
|
|
|
* |
|
388
|
|
|
* @return void |
|
389
|
|
|
*/ |
|
390
|
2 |
|
public function process(LogMessageInterface $logMessage) |
|
391
|
|
|
{ |
|
392
|
|
|
|
|
393
|
|
|
// let the processors process the message |
|
394
|
2 |
|
foreach ($this->getProcessors() as $processor) { |
|
395
|
|
|
$processor->process($logMessage); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
// let the handler log the message |
|
399
|
2 |
|
foreach ($this->getHandlers() as $handler) { |
|
400
|
2 |
|
$handler->handle($logMessage); |
|
401
|
|
|
} |
|
402
|
2 |
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Appends the passed thread context. |
|
406
|
|
|
* |
|
407
|
|
|
* @param string $threadContext The thread context to append |
|
408
|
|
|
* |
|
409
|
|
|
* @return void |
|
410
|
|
|
* @see \AppserverIo\Logger\ThreadSafeLoggerInterface::appendThreadContext() |
|
411
|
|
|
*/ |
|
412
|
|
|
public function appendThreadContext($threadContext) |
|
413
|
|
|
{ |
|
414
|
|
|
Logger::$threadContext = sprintf('%s-%s', Logger::$threadContext, $threadContext); |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|