Logger.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /**
  14. * Monolog log channel
  15. *
  16. * It contains a stack of Handlers and a stack of Processors,
  17. * and uses them to store messages that are added to it.
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class Logger
  22. {
  23. /**
  24. * Debug messages
  25. */
  26. const DEBUG = 100;
  27. /**
  28. * Messages you usually don't want to see
  29. */
  30. const INFO = 200;
  31. /**
  32. * Exceptional occurences that are not errors
  33. *
  34. * This is typically the logging level you want to use
  35. */
  36. const WARNING = 300;
  37. /**
  38. * Errors
  39. */
  40. const ERROR = 400;
  41. protected static $levels = array(
  42. 100 => 'DEBUG',
  43. 200 => 'INFO',
  44. 300 => 'WARNING',
  45. 400 => 'ERROR',
  46. );
  47. protected $name;
  48. /**
  49. * The handler stack
  50. *
  51. * @var array of Monolog\Handler\HandlerInterface
  52. */
  53. protected $handlers = array();
  54. protected $processors = array();
  55. public function __construct($name)
  56. {
  57. $this->name = $name;
  58. }
  59. public function pushHandler(HandlerInterface $handler)
  60. {
  61. array_unshift($this->handlers, $handler);
  62. }
  63. public function popHandler()
  64. {
  65. if (!$this->handlers) {
  66. throw new \LogicException('You tried to pop from an empty handler stack.');
  67. }
  68. return array_shift($this->handlers);
  69. }
  70. public function pushProcessor($callback)
  71. {
  72. array_unshift($this->processors, $callback);
  73. }
  74. public function popProcessor()
  75. {
  76. if (!$this->processors) {
  77. throw new \LogicException('You tried to pop from an empty processor stack.');
  78. }
  79. return array_shift($this->processors);
  80. }
  81. public function addMessage($level, $message)
  82. {
  83. if (!$this->handlers) {
  84. $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
  85. }
  86. $message = array(
  87. 'message' => $message,
  88. 'level' => $level,
  89. 'level_name' => self::getLevelName($level),
  90. 'channel' => $this->name,
  91. 'datetime' => new \DateTime(),
  92. 'extra' => array(),
  93. );
  94. // check if any message will handle this message
  95. $handlerKey = null;
  96. foreach ($this->handlers as $key => $handler) {
  97. if ($handler->isHandling($message)) {
  98. $handlerKey = $key;
  99. break;
  100. }
  101. }
  102. // none found
  103. if (null === $handlerKey) {
  104. return false;
  105. }
  106. // found at least one, process message and dispatch it
  107. foreach ($this->processors as $processor) {
  108. $message = call_user_func($processor, $message, $this);
  109. }
  110. while (isset($this->handlers[$handlerKey]) &&
  111. false === $this->handlers[$handlerKey]->handle($message)) {
  112. $handlerKey++;
  113. }
  114. return true;
  115. }
  116. public function addDebug($message)
  117. {
  118. return $this->addMessage(self::DEBUG, $message);
  119. }
  120. public function addInfo($message)
  121. {
  122. return $this->addMessage(self::INFO, $message);
  123. }
  124. public function addWarning($message)
  125. {
  126. return $this->addMessage(self::WARNING, $message);
  127. }
  128. public function addError($message)
  129. {
  130. return $this->addMessage(self::ERROR, $message);
  131. }
  132. public static function getLevelName($level)
  133. {
  134. return self::$levels[$level];
  135. }
  136. // ZF Logger Compat
  137. public function debug($message)
  138. {
  139. return $this->addMessage(self::DEBUG, $message);
  140. }
  141. public function info($message)
  142. {
  143. return $this->addMessage(self::INFO, $message);
  144. }
  145. public function notice($message)
  146. {
  147. return $this->addMessage(self::INFO, $message);
  148. }
  149. public function warn($message)
  150. {
  151. return $this->addMessage(self::WARNING, $message);
  152. }
  153. public function err($message)
  154. {
  155. return $this->addMessage(self::ERROR, $message);
  156. }
  157. public function crit($message)
  158. {
  159. return $this->addMessage(self::ERROR, $message);
  160. }
  161. public function alert($message)
  162. {
  163. return $this->addMessage(self::ERROR, $message);
  164. }
  165. public function emerg($message)
  166. {
  167. return $this->addMessage(self::ERROR, $message);
  168. }
  169. }