ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
12 namespace Monolog;
13 
18 
27 class Logger implements LoggerInterface
28 {
32  const DEBUG = 100;
33 
39  const INFO = 200;
40 
44  const NOTICE = 250;
45 
52  const WARNING = 300;
53 
57  const ERROR = 400;
58 
64  const CRITICAL = 500;
65 
72  const ALERT = 550;
73 
77  const EMERGENCY = 600;
78 
87  const API = 1;
88 
94  protected static $levels = array(
95  self::DEBUG => 'DEBUG',
96  self::INFO => 'INFO',
97  self::NOTICE => 'NOTICE',
98  self::WARNING => 'WARNING',
99  self::ERROR => 'ERROR',
100  self::CRITICAL => 'CRITICAL',
101  self::ALERT => 'ALERT',
102  self::EMERGENCY => 'EMERGENCY',
103  );
104 
108  protected static $timezone;
109 
113  protected $name;
114 
120  protected $handlers;
121 
129  protected $processors;
130 
134  protected $microsecondTimestamps = true;
135 
141  public function __construct($name, array $handlers = array(), array $processors = array())
142  {
143  $this->name = $name;
144  $this->handlers = $handlers;
145  $this->processors = $processors;
146  }
147 
151  public function getName()
152  {
153  return $this->name;
154  }
155 
161  public function withName($name)
162  {
163  $new = clone $this;
164  $new->name = $name;
165 
166  return $new;
167  }
168 
176  {
177  array_unshift($this->handlers, $handler);
178 
179  return $this;
180  }
181 
187  public function popHandler()
188  {
189  if (!$this->handlers) {
190  throw new \LogicException('You tried to pop from an empty handler stack.');
191  }
192 
193  return array_shift($this->handlers);
194  }
195 
204  public function setHandlers(array $handlers)
205  {
206  $this->handlers = array();
207  foreach (array_reverse($handlers) as $handler) {
208  $this->pushHandler($handler);
209  }
210 
211  return $this;
212  }
213 
217  public function getHandlers()
218  {
219  return $this->handlers;
220  }
221 
228  public function pushProcessor($callback)
229  {
230  if (!is_callable($callback)) {
231  throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
232  }
233  array_unshift($this->processors, $callback);
234 
235  return $this;
236  }
237 
243  public function popProcessor()
244  {
245  if (!$this->processors) {
246  throw new \LogicException('You tried to pop from an empty processor stack.');
247  }
248 
249  return array_shift($this->processors);
250  }
251 
255  public function getProcessors()
256  {
257  return $this->processors;
258  }
259 
273  public function useMicrosecondTimestamps($micro)
274  {
275  $this->microsecondTimestamps = (bool) $micro;
276  }
277 
286  public function addRecord($level, $message, array $context = array())
287  {
288  if (!$this->handlers) {
289  $this->pushHandler(new StreamHandler('php://stderr', static::DEBUG));
290  }
291 
292  $levelName = static::getLevelName($level);
293 
294  // check if any handler will handle this message so we can return early and save cycles
295  $handlerKey = null;
296  reset($this->handlers);
297  while ($handler = current($this->handlers)) {
298  if ($handler->isHandling(array('level' => $level))) {
299  $handlerKey = key($this->handlers);
300  break;
301  }
302 
303  next($this->handlers);
304  }
305 
306  if (null === $handlerKey) {
307  return false;
308  }
309 
310  if (!static::$timezone) {
311  static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');
312  }
313 
314  if ($this->microsecondTimestamps) {
315  $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone);
316  } else {
317  $ts = new \DateTime(null, static::$timezone);
318  }
319  $ts->setTimezone(static::$timezone);
320 
321  $record = array(
322  'message' => (string) $message,
323  'context' => $context,
324  'level' => $level,
325  'level_name' => $levelName,
326  'channel' => $this->name,
327  'datetime' => $ts,
328  'extra' => array(),
329  );
330 
331  foreach ($this->processors as $processor) {
332  $record = call_user_func($processor, $record);
333  }
334 
335  while ($handler = current($this->handlers)) {
336  if (true === $handler->handle($record)) {
337  break;
338  }
339 
340  next($this->handlers);
341  }
342 
343  return true;
344  }
345 
353  public function addDebug($message, array $context = array())
354  {
355  return $this->addRecord(static::DEBUG, $message, $context);
356  }
357 
365  public function addInfo($message, array $context = array())
366  {
367  return $this->addRecord(static::INFO, $message, $context);
368  }
369 
377  public function addNotice($message, array $context = array())
378  {
379  return $this->addRecord(static::NOTICE, $message, $context);
380  }
381 
389  public function addWarning($message, array $context = array())
390  {
391  return $this->addRecord(static::WARNING, $message, $context);
392  }
393 
401  public function addError($message, array $context = array())
402  {
403  return $this->addRecord(static::ERROR, $message, $context);
404  }
405 
413  public function addCritical($message, array $context = array())
414  {
415  return $this->addRecord(static::CRITICAL, $message, $context);
416  }
417 
425  public function addAlert($message, array $context = array())
426  {
427  return $this->addRecord(static::ALERT, $message, $context);
428  }
429 
437  public function addEmergency($message, array $context = array())
438  {
439  return $this->addRecord(static::EMERGENCY, $message, $context);
440  }
441 
447  public static function getLevels()
448  {
449  return array_flip(static::$levels);
450  }
451 
458  public static function getLevelName($level)
459  {
460  if (!isset(static::$levels[$level])) {
461  throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
462  }
463 
464  return static::$levels[$level];
465  }
466 
473  public static function toMonologLevel($level)
474  {
475  if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) {
476  return constant(__CLASS__.'::'.strtoupper($level));
477  }
478 
479  return $level;
480  }
481 
488  public function isHandling($level)
489  {
490  $record = array(
491  'level' => $level,
492  );
493 
494  foreach ($this->handlers as $handler) {
495  if ($handler->isHandling($record)) {
496  return true;
497  }
498  }
499 
500  return false;
501  }
502 
513  public function log($level, $message, array $context = array())
514  {
515  $level = static::toMonologLevel($level);
516 
517  return $this->addRecord($level, $message, $context);
518  }
519 
529  public function debug($message, array $context = array())
530  {
531  return $this->addRecord(static::DEBUG, $message, $context);
532  }
533 
543  public function info($message, array $context = array())
544  {
545  return $this->addRecord(static::INFO, $message, $context);
546  }
547 
557  public function notice($message, array $context = array())
558  {
559  return $this->addRecord(static::NOTICE, $message, $context);
560  }
561 
571  public function warn($message, array $context = array())
572  {
573  return $this->addRecord(static::WARNING, $message, $context);
574  }
575 
585  public function warning($message, array $context = array())
586  {
587  return $this->addRecord(static::WARNING, $message, $context);
588  }
589 
599  public function err($message, array $context = array())
600  {
601  return $this->addRecord(static::ERROR, $message, $context);
602  }
603 
613  public function error($message, array $context = array())
614  {
615  return $this->addRecord(static::ERROR, $message, $context);
616  }
617 
627  public function crit($message, array $context = array())
628  {
629  return $this->addRecord(static::CRITICAL, $message, $context);
630  }
631 
641  public function critical($message, array $context = array())
642  {
643  return $this->addRecord(static::CRITICAL, $message, $context);
644  }
645 
655  public function alert($message, array $context = array())
656  {
657  return $this->addRecord(static::ALERT, $message, $context);
658  }
659 
669  public function emerg($message, array $context = array())
670  {
671  return $this->addRecord(static::EMERGENCY, $message, $context);
672  }
673 
683  public function emergency($message, array $context = array())
684  {
685  return $this->addRecord(static::EMERGENCY, $message, $context);
686  }
687 
695  public static function setTimezone(\DateTimeZone $tz)
696  {
697  self::$timezone = $tz;
698  }
699 }
addDebug($message, array $context=array())
Adds a log record at the DEBUG level.
Definition: Logger.php:353
crit($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:627
addWarning($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:389
warning($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:585
info($message, array $context=array())
Adds a log record at the INFO level.
Definition: Logger.php:543
popProcessor()
Removes the processor on top of the stack and returns it.
Definition: Logger.php:243
err($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:599
addAlert($message, array $context=array())
Adds a log record at the ALERT level.
Definition: Logger.php:425
isHandling($level)
Checks whether the Logger has a handler that listens on the given level.
Definition: Logger.php:488
emergency($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:683
static setTimezone(\DateTimeZone $tz)
Set the timezone to be used for the timestamp of log records.
Definition: Logger.php:695
Monolog log channel.
Definition: Logger.php:27
addEmergency($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:437
log($level, $message, array $context=array())
Adds a log record at an arbitrary level.
Definition: Logger.php:513
setHandlers(array $handlers)
Set handlers, replacing all existing ones.
Definition: Logger.php:204
critical($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:641
static toMonologLevel($level)
Converts PSR-3 levels to Monolog ones if necessary.
Definition: Logger.php:473
alert($message, array $context=array())
Adds a log record at the ALERT level.
Definition: Logger.php:655
addError($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:401
popHandler()
Pops a handler from the stack.
Definition: Logger.php:187
__construct($name, array $handlers=array(), array $processors=array())
Definition: Logger.php:141
debug($message, array $context=array())
Adds a log record at the DEBUG level.
Definition: Logger.php:529
const DEBUG
if($format !==null) $name
Definition: metadata.php:146
pushHandler(HandlerInterface $handler)
Pushes a handler on to the stack.
Definition: Logger.php:175
catch(Exception $e) $message
addNotice($message, array $context=array())
Adds a log record at the NOTICE level.
Definition: Logger.php:377
withName($name)
Return a new cloned instance with the name changed.
Definition: Logger.php:161
emerg($message, array $context=array())
Adds a log record at the EMERGENCY level.
Definition: Logger.php:669
addRecord($level, $message, array $context=array())
Adds a log record.
Definition: Logger.php:286
warn($message, array $context=array())
Adds a log record at the WARNING level.
Definition: Logger.php:571
Create styles array
The data for the language used.
static getLevels()
Gets all supported logging levels.
Definition: Logger.php:447
Describes a logger instance.
useMicrosecondTimestamps($micro)
Control the use of microsecond resolution timestamps in the &#39;datetime&#39; member of new records...
Definition: Logger.php:273
Stores to any stream resource.
notice($message, array $context=array())
Adds a log record at the NOTICE level.
Definition: Logger.php:557
static $timezone
Definition: Logger.php:108
static getLevelName($level)
Gets the name of the logging level.
Definition: Logger.php:458
addCritical($message, array $context=array())
Adds a log record at the CRITICAL level.
Definition: Logger.php:413
error($message, array $context=array())
Adds a log record at the ERROR level.
Definition: Logger.php:613
pushProcessor($callback)
Adds a processor on to the stack.
Definition: Logger.php:228
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
Interface that all Monolog Handlers must implement.
$handler
addInfo($message, array $context=array())
Adds a log record at the INFO level.
Definition: Logger.php:365