ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Logger.php
Go to the documentation of this file.
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
12namespace Monolog;
13
18use Exception;
19
29{
33 const DEBUG = 100;
34
40 const INFO = 200;
41
45 const NOTICE = 250;
46
53 const WARNING = 300;
54
58 const ERROR = 400;
59
65 const CRITICAL = 500;
66
73 const ALERT = 550;
74
78 const EMERGENCY = 600;
79
88 const API = 1;
89
95 protected static $levels = array(
96 self::DEBUG => 'DEBUG',
97 self::INFO => 'INFO',
98 self::NOTICE => 'NOTICE',
99 self::WARNING => 'WARNING',
100 self::ERROR => 'ERROR',
101 self::CRITICAL => 'CRITICAL',
102 self::ALERT => 'ALERT',
103 self::EMERGENCY => 'EMERGENCY',
104 );
105
109 protected static $timezone;
110
114 protected $name;
115
121 protected $handlers;
122
130 protected $processors;
131
135 protected $microsecondTimestamps = true;
136
141
147 public function __construct($name, array $handlers = array(), array $processors = array())
148 {
149 $this->name = $name;
150 $this->setHandlers($handlers);
151 $this->processors = $processors;
152 }
153
157 public function getName()
158 {
159 return $this->name;
160 }
161
167 public function withName($name)
168 {
169 $new = clone $this;
170 $new->name = $name;
171
172 return $new;
173 }
174
182 {
183 array_unshift($this->handlers, $handler);
184
185 return $this;
186 }
187
193 public function popHandler()
194 {
195 if (!$this->handlers) {
196 throw new \LogicException('You tried to pop from an empty handler stack.');
197 }
198
199 return array_shift($this->handlers);
200 }
201
210 public function setHandlers(array $handlers)
211 {
212 $this->handlers = array();
213 foreach (array_reverse($handlers) as $handler) {
214 $this->pushHandler($handler);
215 }
216
217 return $this;
218 }
219
223 public function getHandlers()
224 {
225 return $this->handlers;
226 }
227
234 public function pushProcessor($callback)
235 {
236 if (!is_callable($callback)) {
237 throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
238 }
239 array_unshift($this->processors, $callback);
240
241 return $this;
242 }
243
249 public function popProcessor()
250 {
251 if (!$this->processors) {
252 throw new \LogicException('You tried to pop from an empty processor stack.');
253 }
254
255 return array_shift($this->processors);
256 }
257
261 public function getProcessors()
262 {
263 return $this->processors;
264 }
265
279 public function useMicrosecondTimestamps($micro)
280 {
281 $this->microsecondTimestamps = (bool) $micro;
282 }
283
292 public function addRecord($level, $message, array $context = array())
293 {
294 if (!$this->handlers) {
295 $this->pushHandler(new StreamHandler('php://stderr', static::DEBUG));
296 }
297
298 $levelName = static::getLevelName($level);
299
300 // check if any handler will handle this message so we can return early and save cycles
301 $handlerKey = null;
302 reset($this->handlers);
303 while ($handler = current($this->handlers)) {
304 if ($handler->isHandling(array('level' => $level))) {
305 $handlerKey = key($this->handlers);
306 break;
307 }
308
309 next($this->handlers);
310 }
311
312 if (null === $handlerKey) {
313 return false;
314 }
315
316 if (!static::$timezone) {
317 static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');
318 }
319
320 // php7.1+ always has microseconds enabled, so we do not need this hack
321 if ($this->microsecondTimestamps && PHP_VERSION_ID < 70100) {
322 $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone);
323 } else {
324 $ts = new \DateTime(null, static::$timezone);
325 }
326 $ts->setTimezone(static::$timezone);
327
328 $record = array(
329 'message' => (string) $message,
330 'context' => $context,
331 'level' => $level,
332 'level_name' => $levelName,
333 'channel' => $this->name,
334 'datetime' => $ts,
335 'extra' => array(),
336 );
337
338 try {
339 foreach ($this->processors as $processor) {
340 $record = call_user_func($processor, $record);
341 }
342
343 while ($handler = current($this->handlers)) {
344 if (true === $handler->handle($record)) {
345 break;
346 }
347
348 next($this->handlers);
349 }
350 } catch (Exception $e) {
351 $this->handleException($e, $record);
352 }
353
354 return true;
355 }
356
367 public function close()
368 {
369 foreach ($this->handlers as $handler) {
370 if (method_exists($handler, 'close')) {
371 $handler->close();
372 }
373 }
374 }
375
386 public function reset()
387 {
388 foreach ($this->handlers as $handler) {
389 if ($handler instanceof ResettableInterface) {
390 $handler->reset();
391 }
392 }
393
394 foreach ($this->processors as $processor) {
395 if ($processor instanceof ResettableInterface) {
396 $processor->reset();
397 }
398 }
399 }
400
408 public function addDebug($message, array $context = array())
409 {
410 return $this->addRecord(static::DEBUG, $message, $context);
411 }
412
420 public function addInfo($message, array $context = array())
421 {
422 return $this->addRecord(static::INFO, $message, $context);
423 }
424
432 public function addNotice($message, array $context = array())
433 {
434 return $this->addRecord(static::NOTICE, $message, $context);
435 }
436
444 public function addWarning($message, array $context = array())
445 {
446 return $this->addRecord(static::WARNING, $message, $context);
447 }
448
456 public function addError($message, array $context = array())
457 {
458 return $this->addRecord(static::ERROR, $message, $context);
459 }
460
468 public function addCritical($message, array $context = array())
469 {
470 return $this->addRecord(static::CRITICAL, $message, $context);
471 }
472
480 public function addAlert($message, array $context = array())
481 {
482 return $this->addRecord(static::ALERT, $message, $context);
483 }
484
492 public function addEmergency($message, array $context = array())
493 {
494 return $this->addRecord(static::EMERGENCY, $message, $context);
495 }
496
502 public static function getLevels()
503 {
504 return array_flip(static::$levels);
505 }
506
513 public static function getLevelName($level)
514 {
515 if (!isset(static::$levels[$level])) {
516 throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
517 }
518
519 return static::$levels[$level];
520 }
521
528 public static function toMonologLevel($level)
529 {
530 if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) {
531 return constant(__CLASS__.'::'.strtoupper($level));
532 }
533
534 return $level;
535 }
536
543 public function isHandling($level)
544 {
545 $record = array(
546 'level' => $level,
547 );
548
549 foreach ($this->handlers as $handler) {
550 if ($handler->isHandling($record)) {
551 return true;
552 }
553 }
554
555 return false;
556 }
557
564 public function setExceptionHandler($callback)
565 {
566 if (!is_callable($callback)) {
567 throw new \InvalidArgumentException('Exception handler must be valid callable (callback or object with an __invoke method), '.var_export($callback, true).' given');
568 }
569 $this->exceptionHandler = $callback;
570
571 return $this;
572 }
573
577 public function getExceptionHandler()
578 {
579 return $this->exceptionHandler;
580 }
581
586 protected function handleException(Exception $e, array $record)
587 {
588 if (!$this->exceptionHandler) {
589 throw $e;
590 }
591
592 call_user_func($this->exceptionHandler, $e, $record);
593 }
594
605 public function log($level, $message, array $context = array())
606 {
607 $level = static::toMonologLevel($level);
608
609 return $this->addRecord($level, $message, $context);
610 }
611
621 public function debug($message, array $context = array())
622 {
623 return $this->addRecord(static::DEBUG, $message, $context);
624 }
625
635 public function info($message, array $context = array())
636 {
637 return $this->addRecord(static::INFO, $message, $context);
638 }
639
649 public function notice($message, array $context = array())
650 {
651 return $this->addRecord(static::NOTICE, $message, $context);
652 }
653
663 public function warn($message, array $context = array())
664 {
665 return $this->addRecord(static::WARNING, $message, $context);
666 }
667
677 public function warning($message, array $context = array())
678 {
679 return $this->addRecord(static::WARNING, $message, $context);
680 }
681
691 public function err($message, array $context = array())
692 {
693 return $this->addRecord(static::ERROR, $message, $context);
694 }
695
705 public function error($message, array $context = array())
706 {
707 return $this->addRecord(static::ERROR, $message, $context);
708 }
709
719 public function crit($message, array $context = array())
720 {
721 return $this->addRecord(static::CRITICAL, $message, $context);
722 }
723
733 public function critical($message, array $context = array())
734 {
735 return $this->addRecord(static::CRITICAL, $message, $context);
736 }
737
747 public function alert($message, array $context = array())
748 {
749 return $this->addRecord(static::ALERT, $message, $context);
750 }
751
761 public function emerg($message, array $context = array())
762 {
763 return $this->addRecord(static::EMERGENCY, $message, $context);
764 }
765
775 public function emergency($message, array $context = array())
776 {
777 return $this->addRecord(static::EMERGENCY, $message, $context);
778 }
779
787 public static function setTimezone(\DateTimeZone $tz)
788 {
789 self::$timezone = $tz;
790 }
791}
An exception for terminatinating execution or to throw for unit testing.
Stores to any stream resource.
Monolog log channel.
Definition: Logger.php:29
addEmergency($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:492
addRecord($level, $message, array $context=array())
Adds a log record.
Definition: Logger.php:292
emergency($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:775
pushHandler(HandlerInterface $handler)
Pushes a handler on to the stack.
Definition: Logger.php:181
setHandlers(array $handlers)
Set handlers, replacing all existing ones.
Definition: Logger.php:210
setExceptionHandler($callback)
Set a custom exception handler.
Definition: Logger.php:564
warning($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:677
err($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:691
crit($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:719
pushProcessor($callback)
Adds a processor on to the stack.
Definition: Logger.php:234
static toMonologLevel($level)
Converts PSR-3 levels to Monolog ones if necessary.
Definition: Logger.php:528
getExceptionHandler()
Definition: Logger.php:577
useMicrosecondTimestamps($micro)
Control the use of microsecond resolution timestamps in the 'datetime' member of new records.
Definition: Logger.php:279
static $timezone
Definition: Logger.php:109
addInfo($message, array $context=array())
Adds a log record at the INFO level.
Definition: Logger.php:420
info($message, array $context=array())
Adds a log record at the INFO level.
Definition: Logger.php:635
static getLevelName($level)
Gets the name of the logging level.
Definition: Logger.php:513
reset()
Ends a log cycle and resets all handlers and processors to their initial state.
Definition: Logger.php:386
log($level, $message, array $context=array())
Adds a log record at an arbitrary level.
Definition: Logger.php:605
notice($message, array $context=array())
Adds a log record at the NOTICE level.
Definition: Logger.php:649
debug($message, array $context=array())
Adds a log record at the DEBUG level.
Definition: Logger.php:621
popProcessor()
Removes the processor on top of the stack and returns it.
Definition: Logger.php:249
__construct($name, array $handlers=array(), array $processors=array())
Definition: Logger.php:147
static getLevels()
Gets all supported logging levels.
Definition: Logger.php:502
handleException(Exception $e, array $record)
Delegates exception management to the custom exception handler, or throws the exception if no custom ...
Definition: Logger.php:586
critical($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:733
addAlert($message, array $context=array())
Adds a log record at the ALERT level.
Definition: Logger.php:480
emerg($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:761
isHandling($level)
Checks whether the Logger has a handler that listens on the given level.
Definition: Logger.php:543
alert($message, array $context=array())
Adds a log record at the ALERT level.
Definition: Logger.php:747
popHandler()
Pops a handler from the stack.
Definition: Logger.php:193
close()
Ends a log cycle and frees all resources used by handlers.
Definition: Logger.php:367
addDebug($message, array $context=array())
Adds a log record at the DEBUG level.
Definition: Logger.php:408
error($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:705
addError($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:456
addCritical($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:468
addNotice($message, array $context=array())
Adds a log record at the NOTICE level.
Definition: Logger.php:432
withName($name)
Return a new cloned instance with the name changed.
Definition: Logger.php:167
static setTimezone(\DateTimeZone $tz)
Set the timezone to be used for the timestamp of log records.
Definition: Logger.php:787
warn($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:663
addWarning($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:444
const DEBUG
Interface that all Monolog Handlers must implement.
Handler or Processor implementing this interface will be reset when Logger::reset() is called.
Describes a logger instance.
catch(Exception $e) $message
$handler
$context
Definition: webdav.php:25