Logger.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog;
  11. use Monolog\Handler\HandlerInterface;
  12. use Monolog\Handler\StreamHandler;
  13. class Logger
  14. {
  15. /**
  16. * Debug messages
  17. */
  18. const DEBUG = 100;
  19. /**
  20. * Messages you usually don't want to see
  21. */
  22. const INFO = 200;
  23. /**
  24. * Exceptional occurences that are not errors
  25. *
  26. * This is typically the logging level you want to use
  27. */
  28. const WARNING = 300;
  29. /**
  30. * Errors
  31. */
  32. const ERROR = 400;
  33. protected static $levels = array(
  34. 100 => 'DEBUG',
  35. 200 => 'INFO',
  36. 300 => 'WARNING',
  37. 400 => 'ERROR',
  38. );
  39. protected $name;
  40. /**
  41. * The handler instance at the top of the handler stack
  42. *
  43. * @var Monolog\Handler\HandlerInterface
  44. */
  45. protected $handler;
  46. protected $processors = array();
  47. public function __construct($name)
  48. {
  49. $this->name = $name;
  50. }
  51. public function pushHandler(HandlerInterface $handler)
  52. {
  53. if ($this->handler) {
  54. $handler->setParent($this->handler);
  55. }
  56. $this->handler = $handler;
  57. }
  58. public function popHandler()
  59. {
  60. if (null === $this->handler) {
  61. throw new \LogicException('You tried to pop from an empty handler stack.');
  62. }
  63. $top = $this->handler;
  64. $this->handler = $top->getParent();
  65. return $top;
  66. }
  67. public function pushProcessor($callback)
  68. {
  69. $this->processors[] = $callback;
  70. }
  71. public function popProcessor()
  72. {
  73. return array_pop($this->processors);
  74. }
  75. public function addMessage($level, $message)
  76. {
  77. if (null === $this->handler) {
  78. $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
  79. }
  80. $message = array(
  81. 'message' => $message,
  82. 'level' => $level,
  83. 'level_name' => self::getLevelName($level),
  84. 'channel' => $this->name,
  85. 'datetime' => new \DateTime(),
  86. 'extra' => array(),
  87. );
  88. $handler = $this->handler->getHandler($message);
  89. if (!$handler) {
  90. return false;
  91. }
  92. foreach ($this->processors as $processor) {
  93. $message = call_user_func($processor, $message, $this);
  94. }
  95. $handler->handle($message);
  96. return true;
  97. }
  98. public function addDebug($message)
  99. {
  100. return $this->addMessage(self::DEBUG, $message);
  101. }
  102. public function addInfo($message)
  103. {
  104. return $this->addMessage(self::INFO, $message);
  105. }
  106. public function addWarning($message)
  107. {
  108. return $this->addMessage(self::WARNING, $message);
  109. }
  110. public function addError($message)
  111. {
  112. return $this->addMessage(self::ERROR, $message);
  113. }
  114. public static function getLevelName($level)
  115. {
  116. return self::$levels[$level];
  117. }
  118. // ZF Logger Compat
  119. public function debug($message)
  120. {
  121. return $this->addMessage(self::DEBUG, $message);
  122. }
  123. public function info($message)
  124. {
  125. return $this->addMessage(self::INFO, $message);
  126. }
  127. public function notice($message)
  128. {
  129. return $this->addMessage(self::INFO, $message);
  130. }
  131. public function warn($message)
  132. {
  133. return $this->addMessage(self::WARNING, $message);
  134. }
  135. public function err($message)
  136. {
  137. return $this->addMessage(self::ERROR, $message);
  138. }
  139. public function crit($message)
  140. {
  141. return $this->addMessage(self::ERROR, $message);
  142. }
  143. public function alert($message)
  144. {
  145. return $this->addMessage(self::ERROR, $message);
  146. }
  147. public function emerg($message)
  148. {
  149. return $this->addMessage(self::ERROR, $message);
  150. }
  151. }