Logger.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 records 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. /**
  56. * @param string $name The logging channel
  57. */
  58. public function __construct($name)
  59. {
  60. $this->name = $name;
  61. }
  62. /**
  63. * Pushes an handler on the stack.
  64. *
  65. * @param HandlerInterface $handler
  66. */
  67. public function pushHandler(HandlerInterface $handler)
  68. {
  69. array_unshift($this->handlers, $handler);
  70. }
  71. /**
  72. * Pops an handler from the stack
  73. *
  74. * @return HandlerInterface
  75. */
  76. public function popHandler()
  77. {
  78. if (!$this->handlers) {
  79. throw new \LogicException('You tried to pop from an empty handler stack.');
  80. }
  81. return array_shift($this->handlers);
  82. }
  83. /**
  84. * Adds a processor in the stack.
  85. *
  86. * @param callable $callback
  87. */
  88. public function pushProcessor($callback)
  89. {
  90. array_unshift($this->processors, $callback);
  91. }
  92. /**
  93. * Removes the processor on top of the stack and returns it.
  94. *
  95. * @return callable
  96. */
  97. public function popProcessor()
  98. {
  99. if (!$this->processors) {
  100. throw new \LogicException('You tried to pop from an empty processor stack.');
  101. }
  102. return array_shift($this->processors);
  103. }
  104. /**
  105. * Adds a log record.
  106. *
  107. * @param integer $level The logging level
  108. * @param string $message The log message
  109. * @return Boolean Whether the record has been processed
  110. */
  111. public function addRecord($level, $message)
  112. {
  113. if (!$this->handlers) {
  114. $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
  115. }
  116. $record = array(
  117. 'message' => $message,
  118. 'level' => $level,
  119. 'level_name' => self::getLevelName($level),
  120. 'channel' => $this->name,
  121. 'datetime' => new \DateTime(),
  122. 'extra' => array(),
  123. );
  124. // check if any message will handle this message
  125. $handlerKey = null;
  126. foreach ($this->handlers as $key => $handler) {
  127. if ($handler->isHandling($record)) {
  128. $handlerKey = $key;
  129. break;
  130. }
  131. }
  132. // none found
  133. if (null === $handlerKey) {
  134. return false;
  135. }
  136. // found at least one, process message and dispatch it
  137. foreach ($this->processors as $processor) {
  138. $record = call_user_func($processor, $record, $this);
  139. }
  140. while (isset($this->handlers[$handlerKey]) &&
  141. false === $this->handlers[$handlerKey]->handle($record)) {
  142. $handlerKey++;
  143. }
  144. return true;
  145. }
  146. /**
  147. * Adds a log record at the DEBUG level.
  148. *
  149. * @param string $message The log message
  150. * @return Boolean Whether the record has been processed
  151. */
  152. public function addDebug($message)
  153. {
  154. return $this->addRecord(self::DEBUG, $message);
  155. }
  156. /**
  157. * Adds a log record at the INFO level.
  158. *
  159. * @param string $message The log message
  160. * @return Boolean Whether the record has been processed
  161. */
  162. public function addInfo($message)
  163. {
  164. return $this->addRecord(self::INFO, $message);
  165. }
  166. /**
  167. * Adds a log record at the WARNING level.
  168. *
  169. * @param string $message The log message
  170. * @return Boolean Whether the record has been processed
  171. */
  172. public function addWarning($message)
  173. {
  174. return $this->addRecord(self::WARNING, $message);
  175. }
  176. /**
  177. * Adds a log record at the ERROR level.
  178. *
  179. * @param string $message The log message
  180. * @return Boolean Whether the record has been processed
  181. */
  182. public function addError($message)
  183. {
  184. return $this->addRecord(self::ERROR, $message);
  185. }
  186. /**
  187. * Gets the name of the logging level.
  188. *
  189. * @param integer $level
  190. * @return string
  191. */
  192. public static function getLevelName($level)
  193. {
  194. return self::$levels[$level];
  195. }
  196. // ZF Logger Compat
  197. /**
  198. * Adds a log record at the DEBUG level.
  199. *
  200. * This method allows to have an esay ZF compatibility.
  201. *
  202. * @param string $message The log message
  203. * @return Boolean Whether the record has been processed
  204. */
  205. public function debug($message)
  206. {
  207. return $this->addRecord(self::DEBUG, $message);
  208. }
  209. /**
  210. * Adds a log record at the INFO level.
  211. *
  212. * This method allows to have an esay ZF compatibility.
  213. *
  214. * @param string $message The log message
  215. * @return Boolean Whether the record has been processed
  216. */
  217. public function info($message)
  218. {
  219. return $this->addRecord(self::INFO, $message);
  220. }
  221. /**
  222. * Adds a log record at the INFO level.
  223. *
  224. * This method allows to have an esay ZF compatibility.
  225. *
  226. * @param string $message The log message
  227. * @return Boolean Whether the record has been processed
  228. */
  229. public function notice($message)
  230. {
  231. return $this->addRecord(self::INFO, $message);
  232. }
  233. /**
  234. * Adds a log record at the WARNING level.
  235. *
  236. * This method allows to have an esay ZF compatibility.
  237. *
  238. * @param string $message The log message
  239. * @return Boolean Whether the record has been processed
  240. */
  241. public function warn($message)
  242. {
  243. return $this->addRecord(self::WARNING, $message);
  244. }
  245. /**
  246. * Adds a log record at the ERROR level.
  247. *
  248. * This method allows to have an esay ZF compatibility.
  249. *
  250. * @param string $message The log message
  251. * @return Boolean Whether the record has been processed
  252. */
  253. public function err($message)
  254. {
  255. return $this->addRecord(self::ERROR, $message);
  256. }
  257. /**
  258. * Adds a log record at the ERROR level.
  259. *
  260. * This method allows to have an esay ZF compatibility.
  261. *
  262. * @param string $message The log message
  263. * @return Boolean Whether the record has been processed
  264. */
  265. public function crit($message)
  266. {
  267. return $this->addRecord(self::ERROR, $message);
  268. }
  269. /**
  270. * Adds a log record at the ERROR level.
  271. *
  272. * This method allows to have an esay ZF compatibility.
  273. *
  274. * @param string $message The log message
  275. * @return Boolean Whether the record has been processed
  276. */
  277. public function alert($message)
  278. {
  279. return $this->addRecord(self::ERROR, $message);
  280. }
  281. /**
  282. * Adds a log record at the ERROR level.
  283. *
  284. * This method allows to have an esay ZF compatibility.
  285. *
  286. * @param string $message The log message
  287. * @return Boolean Whether the record has been processed
  288. */
  289. public function emerg($message)
  290. {
  291. return $this->addRecord(self::ERROR, $message);
  292. }
  293. }