PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Monolog / Logger.php

Logger.php in ManageWP Worker 4.9.25, at src/Monolog/Logger.php

610 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 /**
13 * Monolog log channel
14 *
15 * It contains a stack of Handlers and a stack of Processors,
16 * and uses them to store records that are added to it.
17 *
18 * @author Jordi Boggiano <j.boggiano@seld.be>
19 */
20 class Monolog_Logger implements Monolog_Psr_LoggerInterface
21 {
22 /**
23 * Detailed debug information
24 */
25 const DEBUG = 100;
26
27 /**
28 * Interesting events
29 *
30 * Examples: User logs in, SQL logs.
31 */
32 const INFO = 200;
33
34 /**
35 * Uncommon events
36 */
37 const NOTICE = 250;
38
39 /**
40 * Exceptional occurrences that are not errors
41 *
42 * Examples: Use of deprecated APIs, poor use of an API,
43 * undesirable things that are not necessarily wrong.
44 */
45 const WARNING = 300;
46
47 /**
48 * Runtime errors
49 */
50 const ERROR = 400;
51
52 /**
53 * Critical conditions
54 *
55 * Example: Application component unavailable, unexpected exception.
56 */
57 const CRITICAL = 500;
58
59 /**
60 * Action must be taken immediately
61 *
62 * Example: Entire website down, database unavailable, etc.
63 * This should trigger the SMS alerts and wake you up.
64 */
65 const ALERT = 550;
66
67 /**
68 * Urgent alert.
69 */
70 const EMERGENCY = 600;
71
72 /**
73 * Monolog API version
74 *
75 * This is only bumped when API breaks are done and should
76 * follow the major version of the library
77 *
78 * @var int
79 */
80 const API = 1;
81
82 /**
83 * Logging levels from syslog protocol defined in RFC 5424
84 *
85 * @var array $levels Logging levels
86 */
87 protected static $levels = array(
88 100 => 'DEBUG',
89 200 => 'INFO',
90 250 => 'NOTICE',
91 300 => 'WARNING',
92 400 => 'ERROR',
93 500 => 'CRITICAL',
94 550 => 'ALERT',
95 600 => 'EMERGENCY',
96 );
97
98 /**
99 * @var DateTimeZone
100 */
101 protected static $timezone;
102
103 /**
104 * @var string
105 */
106 protected $name;
107
108 /**
109 * The handler stack
110 *
111 * @var Monolog_Handler_HandlerInterface[]
112 */
113 protected $handlers;
114
115 /**
116 * Processors that will process all log records
117 *
118 * To process records of a single handler instead, add the processor on that specific handler
119 *
120 * @var callable[]
121 */
122 protected $processors;
123
124 /**
125 * @param string $name The logging channel
126 * @param Monolog_Handler_HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first, etc.
127 * @param callable[] $processors Optional array of processors
128 */
129 public function __construct($name, array $handlers = array(), array $processors = array())
130 {
131 $this->name = $name;
132 $this->handlers = $handlers;
133 $this->processors = $processors;
134 }
135
136 /**
137 * @return string
138 */
139 public function getName()
140 {
141 return $this->name;
142 }
143
144 /**
145 * Pushes a handler on to the stack.
146 *
147 * @param Monolog_Handler_HandlerInterface $handler
148 */
149 public function pushHandler(Monolog_Handler_HandlerInterface $handler)
150 {
151 array_unshift($this->handlers, $handler);
152 }
153
154 /**
155 * Pops a handler from the stack
156 *
157 * @return Monolog_Handler_HandlerInterface
158 */
159 public function popHandler()
160 {
161 if (!$this->handlers) {
162 throw new LogicException('You tried to pop from an empty handler stack.');
163 }
164
165 return array_shift($this->handlers);
166 }
167
168 /**
169 * Adds a processor on to the stack.
170 *
171 * @param callable $callback
172 */
173 public function pushProcessor($callback)
174 {
175 if (!is_callable($callback)) {
176 throw new InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
177 }
178 array_unshift($this->processors, $callback);
179 }
180
181 /**
182 * Removes the processor on top of the stack and returns it.
183 *
184 * @return callable
185 */
186 public function popProcessor()
187 {
188 if (!$this->processors) {
189 throw new LogicException('You tried to pop from an empty processor stack.');
190 }
191
192 return array_shift($this->processors);
193 }
194
195 /**
196 * Adds a log record.
197 *
198 * @param integer $level The logging level
199 * @param string $message The log message
200 * @param array $context The log context
201 *
202 * @return Boolean Whether the record has been processed
203 */
204 public function addRecord($level, $message, array $context = array())
205 {
206 if (!$this->handlers) {
207 $this->pushHandler(new Monolog_Handler_StreamHandler('php://stderr', self::DEBUG));
208 }
209
210 $record = array(
211 'message' => (string) $message,
212 'context' => $context,
213 'level' => $level,
214 'level_name' => self::getLevelName($level),
215 'channel' => $this->name,
216 'datetime' => $this->getCurrentTimestamp(),
217 'extra' => array(),
218 );
219 // check if any handler will handle this message
220 $handlerKey = null;
221 foreach ($this->handlers as $key => $handler) {
222 if ($handler->isHandling($record)) {
223 $handlerKey = $key;
224 break;
225 }
226 }
227 // none found
228 if (null === $handlerKey) {
229 return false;
230 }
231
232 // found at least one, process message and dispatch it
233 foreach ($this->processors as $processor) {
234 $record = call_user_func($processor, $record);
235 }
236 while (isset($this->handlers[$handlerKey]) &&
237 false === $this->handlers[$handlerKey]->handle($record)) {
238 $handlerKey++;
239 }
240
241 return true;
242 }
243
244 private function getCurrentTimestamp()
245 {
246 if (!self::$timezone) {
247 self::$timezone = new DateTimeZone(date_default_timezone_get() ? date_default_timezone_get() : 'UTC');
248 }
249
250 if (is_callable(array('DateTime', 'createFromFormat'))) {
251 /** @handled static */
252 return DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), self::$timezone)->setTimezone(self::$timezone);
253 }
254
255 return new DateTime('now', self::$timezone);
256 }
257
258 /**
259 * Adds a log record at the DEBUG level.
260 *
261 * @param string $message The log message
262 * @param array $context The log context
263 *
264 * @return Boolean Whether the record has been processed
265 */
266 public function addDebug($message, array $context = array())
267 {
268 return $this->addRecord(self::DEBUG, $message, $context);
269 }
270
271 /**
272 * Adds a log record at the INFO level.
273 *
274 * @param string $message The log message
275 * @param array $context The log context
276 *
277 * @return Boolean Whether the record has been processed
278 */
279 public function addInfo($message, array $context = array())
280 {
281 return $this->addRecord(self::INFO, $message, $context);
282 }
283
284 /**
285 * Adds a log record at the NOTICE level.
286 *
287 * @param string $message The log message
288 * @param array $context The log context
289 *
290 * @return Boolean Whether the record has been processed
291 */
292 public function addNotice($message, array $context = array())
293 {
294 return $this->addRecord(self::NOTICE, $message, $context);
295 }
296
297 /**
298 * Adds a log record at the WARNING level.
299 *
300 * @param string $message The log message
301 * @param array $context The log context
302 *
303 * @return Boolean Whether the record has been processed
304 */
305 public function addWarning($message, array $context = array())
306 {
307 return $this->addRecord(self::WARNING, $message, $context);
308 }
309
310 /**
311 * Adds a log record at the ERROR level.
312 *
313 * @param string $message The log message
314 * @param array $context The log context
315 *
316 * @return Boolean Whether the record has been processed
317 */
318 public function addError($message, array $context = array())
319 {
320 return $this->addRecord(self::ERROR, $message, $context);
321 }
322
323 /**
324 * Adds a log record at the CRITICAL level.
325 *
326 * @param string $message The log message
327 * @param array $context The log context
328 *
329 * @return Boolean Whether the record has been processed
330 */
331 public function addCritical($message, array $context = array())
332 {
333 return $this->addRecord(self::CRITICAL, $message, $context);
334 }
335
336 /**
337 * Adds a log record at the ALERT level.
338 *
339 * @param string $message The log message
340 * @param array $context The log context
341 *
342 * @return Boolean Whether the record has been processed
343 */
344 public function addAlert($message, array $context = array())
345 {
346 return $this->addRecord(self::ALERT, $message, $context);
347 }
348
349 /**
350 * Adds a log record at the EMERGENCY level.
351 *
352 * @param string $message The log message
353 * @param array $context The log context
354 *
355 * @return Boolean Whether the record has been processed
356 */
357 public function addEmergency($message, array $context = array())
358 {
359 return $this->addRecord(self::EMERGENCY, $message, $context);
360 }
361
362 /**
363 * Gets all supported logging levels.
364 *
365 * @return array Assoc array with human-readable level names => level codes.
366 */
367 public static function getLevels()
368 {
369 return array_flip(self::$levels);
370 }
371
372 /**
373 * Gets the name of the logging level.
374 *
375 * @param integer $level
376 *
377 * @return string
378 */
379 public static function getLevelName($level)
380 {
381 if (!isset(self::$levels[$level])) {
382 throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(self::$levels)));
383 }
384
385 return self::$levels[$level];
386 }
387
388 /**
389 * Checks whether the Logger has a handler that listens on the given level
390 *
391 * @param integer $level
392 *
393 * @return Boolean
394 */
395 public function isHandling($level)
396 {
397 $record = array(
398 'level' => $level,
399 );
400
401 foreach ($this->handlers as $handler) {
402 if ($handler->isHandling($record)) {
403 return true;
404 }
405 }
406
407 return false;
408 }
409
410 /**
411 * Adds a log record at an arbitrary level.
412 *
413 * This method allows for compatibility with common interfaces.
414 *
415 * @param mixed $level The log level
416 * @param string $message The log message
417 * @param array $context The log context
418 *
419 * @return Boolean Whether the record has been processed
420 */
421 public function log($level, $message, array $context = array())
422 {
423 if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) {
424 $level = constant(__CLASS__.'::'.strtoupper($level));
425 }
426
427 return $this->addRecord($level, $message, $context);
428 }
429
430 /**
431 * Adds a log record at the DEBUG level.
432 *
433 * This method allows for compatibility with common interfaces.
434 *
435 * @param string $message The log message
436 * @param array $context The log context
437 *
438 * @return Boolean Whether the record has been processed
439 */
440 public function debug($message, array $context = array())
441 {
442 return $this->addRecord(self::DEBUG, $message, $context);
443 }
444
445 /**
446 * Adds a log record at the INFO level.
447 *
448 * This method allows for compatibility with common interfaces.
449 *
450 * @param string $message The log message
451 * @param array $context The log context
452 *
453 * @return Boolean Whether the record has been processed
454 */
455 public function info($message, array $context = array())
456 {
457 return $this->addRecord(self::INFO, $message, $context);
458 }
459
460 /**
461 * Adds a log record at the INFO level.
462 *
463 * This method allows for compatibility with common interfaces.
464 *
465 * @param string $message The log message
466 * @param array $context The log context
467 *
468 * @return Boolean Whether the record has been processed
469 */
470 public function notice($message, array $context = array())
471 {
472 return $this->addRecord(self::NOTICE, $message, $context);
473 }
474
475 /**
476 * Adds a log record at the WARNING level.
477 *
478 * This method allows for compatibility with common interfaces.
479 *
480 * @param string $message The log message
481 * @param array $context The log context
482 *
483 * @return Boolean Whether the record has been processed
484 */
485 public function warn($message, array $context = array())
486 {
487 return $this->addRecord(self::WARNING, $message, $context);
488 }
489
490 /**
491 * Adds a log record at the WARNING level.
492 *
493 * This method allows for compatibility with common interfaces.
494 *
495 * @param string $message The log message
496 * @param array $context The log context
497 *
498 * @return Boolean Whether the record has been processed
499 */
500 public function warning($message, array $context = array())
501 {
502 return $this->addRecord(self::WARNING, $message, $context);
503 }
504
505 /**
506 * Adds a log record at the ERROR level.
507 *
508 * This method allows for compatibility with common interfaces.
509 *
510 * @param string $message The log message
511 * @param array $context The log context
512 *
513 * @return Boolean Whether the record has been processed
514 */
515 public function err($message, array $context = array())
516 {
517 return $this->addRecord(self::ERROR, $message, $context);
518 }
519
520 /**
521 * Adds a log record at the ERROR level.
522 *
523 * This method allows for compatibility with common interfaces.
524 *
525 * @param string $message The log message
526 * @param array $context The log context
527 *
528 * @return Boolean Whether the record has been processed
529 */
530 public function error($message, array $context = array())
531 {
532 return $this->addRecord(self::ERROR, $message, $context);
533 }
534
535 /**
536 * Adds a log record at the CRITICAL level.
537 *
538 * This method allows for compatibility with common interfaces.
539 *
540 * @param string $message The log message
541 * @param array $context The log context
542 *
543 * @return Boolean Whether the record has been processed
544 */
545 public function crit($message, array $context = array())
546 {
547 return $this->addRecord(self::CRITICAL, $message, $context);
548 }
549
550 /**
551 * Adds a log record at the CRITICAL level.
552 *
553 * This method allows for compatibility with common interfaces.
554 *
555 * @param string $message The log message
556 * @param array $context The log context
557 *
558 * @return Boolean Whether the record has been processed
559 */
560 public function critical($message, array $context = array())
561 {
562 return $this->addRecord(self::CRITICAL, $message, $context);
563 }
564
565 /**
566 * Adds a log record at the ALERT level.
567 *
568 * This method allows for compatibility with common interfaces.
569 *
570 * @param string $message The log message
571 * @param array $context The log context
572 *
573 * @return Boolean Whether the record has been processed
574 */
575 public function alert($message, array $context = array())
576 {
577 return $this->addRecord(self::ALERT, $message, $context);
578 }
579
580 /**
581 * Adds a log record at the EMERGENCY level.
582 *
583 * This method allows for compatibility with common interfaces.
584 *
585 * @param string $message The log message
586 * @param array $context The log context
587 *
588 * @return Boolean Whether the record has been processed
589 */
590 public function emerg($message, array $context = array())
591 {
592 return $this->addRecord(self::EMERGENCY, $message, $context);
593 }
594
595 /**
596 * Adds a log record at the EMERGENCY level.
597 *
598 * This method allows for compatibility with common interfaces.
599 *
600 * @param string $message The log message
601 * @param array $context The log context
602 *
603 * @return Boolean Whether the record has been processed
604 */
605 public function emergency($message, array $context = array())
606 {
607 return $this->addRecord(self::EMERGENCY, $message, $context);
608 }
609 }
610