PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/monolog/src/Monolog/Logger.php

https://github.com/unglud/You-give-me-Ill-give-you
PHP | 393 lines | 149 code | 37 blank | 207 comment | 7 complexity | 547826002051f4fcaf5d6163390274a0 MD5 | raw file
  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. * Detailed debug information
  25. */
  26. const DEBUG = 100;
  27. /**
  28. * Interesting events
  29. *
  30. * Examples: User logs in, SQL logs.
  31. */
  32. const INFO = 200;
  33. /**
  34. * Exceptional occurences that are not errors
  35. *
  36. * Examples: Use of deprecated APIs, poor use of an API,
  37. * undesirable things that are not necessarily wrong.
  38. */
  39. const WARNING = 300;
  40. /**
  41. * Runtime errors
  42. */
  43. const ERROR = 400;
  44. /**
  45. * Critical conditions
  46. *
  47. * Example: Application component unavailable, unexpected exception.
  48. */
  49. const CRITICAL = 500;
  50. /**
  51. * Action must be taken immediately
  52. *
  53. * Example: Entire website down, database unavailable, etc.
  54. * This should trigger the SMS alerts and wake you up.
  55. */
  56. const ALERT = 550;
  57. protected static $levels = array(
  58. 100 => 'DEBUG',
  59. 200 => 'INFO',
  60. 300 => 'WARNING',
  61. 400 => 'ERROR',
  62. 500 => 'CRITICAL',
  63. 550 => 'ALERT',
  64. );
  65. protected $name;
  66. /**
  67. * The handler stack
  68. *
  69. * @var array of Monolog\Handler\HandlerInterface
  70. */
  71. protected $handlers = array();
  72. protected $processors = array();
  73. /**
  74. * @param string $name The logging channel
  75. */
  76. public function __construct($name)
  77. {
  78. $this->name = $name;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getName() {
  84. return $this->name;
  85. }
  86. /**
  87. * Pushes an handler on the stack.
  88. *
  89. * @param HandlerInterface $handler
  90. */
  91. public function pushHandler(HandlerInterface $handler)
  92. {
  93. array_unshift($this->handlers, $handler);
  94. }
  95. /**
  96. * Pops an handler from the stack
  97. *
  98. * @return HandlerInterface
  99. */
  100. public function popHandler()
  101. {
  102. if (!$this->handlers) {
  103. throw new \LogicException('You tried to pop from an empty handler stack.');
  104. }
  105. return array_shift($this->handlers);
  106. }
  107. /**
  108. * Adds a processor in the stack.
  109. *
  110. * @param callable $callback
  111. */
  112. public function pushProcessor($callback)
  113. {
  114. if (!is_callable($callback)) {
  115. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
  116. }
  117. array_unshift($this->processors, $callback);
  118. }
  119. /**
  120. * Removes the processor on top of the stack and returns it.
  121. *
  122. * @return callable
  123. */
  124. public function popProcessor()
  125. {
  126. if (!$this->processors) {
  127. throw new \LogicException('You tried to pop from an empty processor stack.');
  128. }
  129. return array_shift($this->processors);
  130. }
  131. /**
  132. * Adds a log record.
  133. *
  134. * @param integer $level The logging level
  135. * @param string $message The log message
  136. * @param array $context The log context
  137. * @return Boolean Whether the record has been processed
  138. */
  139. public function addRecord($level, $message, array $context = array())
  140. {
  141. if (!$this->handlers) {
  142. $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
  143. }
  144. $record = array(
  145. 'message' => (string) $message,
  146. 'context' => $context,
  147. 'level' => $level,
  148. 'level_name' => self::getLevelName($level),
  149. 'channel' => $this->name,
  150. 'datetime' => new \DateTime(),
  151. 'extra' => array(),
  152. );
  153. // check if any message will handle this message
  154. $handlerKey = null;
  155. foreach ($this->handlers as $key => $handler) {
  156. if ($handler->isHandling($record)) {
  157. $handlerKey = $key;
  158. break;
  159. }
  160. }
  161. // none found
  162. if (null === $handlerKey) {
  163. return false;
  164. }
  165. // found at least one, process message and dispatch it
  166. foreach ($this->processors as $processor) {
  167. $record = call_user_func($processor, $record);
  168. }
  169. while (isset($this->handlers[$handlerKey]) &&
  170. false === $this->handlers[$handlerKey]->handle($record)) {
  171. $handlerKey++;
  172. }
  173. return true;
  174. }
  175. /**
  176. * Adds a log record at the DEBUG level.
  177. *
  178. * @param string $message The log message
  179. * @param array $context The log context
  180. * @return Boolean Whether the record has been processed
  181. */
  182. public function addDebug($message, array $context = array())
  183. {
  184. return $this->addRecord(self::DEBUG, $message, $context);
  185. }
  186. /**
  187. * Adds a log record at the INFO level.
  188. *
  189. * @param string $message The log message
  190. * @param array $context The log context
  191. * @return Boolean Whether the record has been processed
  192. */
  193. public function addInfo($message, array $context = array())
  194. {
  195. return $this->addRecord(self::INFO, $message, $context);
  196. }
  197. /**
  198. * Adds a log record at the WARNING level.
  199. *
  200. * @param string $message The log message
  201. * @param array $context The log context
  202. * @return Boolean Whether the record has been processed
  203. */
  204. public function addWarning($message, array $context = array())
  205. {
  206. return $this->addRecord(self::WARNING, $message, $context);
  207. }
  208. /**
  209. * Adds a log record at the ERROR level.
  210. *
  211. * @param string $message The log message
  212. * @param array $context The log context
  213. * @return Boolean Whether the record has been processed
  214. */
  215. public function addError($message, array $context = array())
  216. {
  217. return $this->addRecord(self::ERROR, $message, $context);
  218. }
  219. /**
  220. * Adds a log record at the CRITICAL level.
  221. *
  222. * @param string $message The log message
  223. * @param array $context The log context
  224. * @return Boolean Whether the record has been processed
  225. */
  226. public function addCritical($message, array $context = array())
  227. {
  228. return $this->addRecord(self::CRITICAL, $message, $context);
  229. }
  230. /**
  231. * Adds a log record at the ALERT level.
  232. *
  233. * @param string $message The log message
  234. * @param array $context The log context
  235. * @return Boolean Whether the record has been processed
  236. */
  237. public function addAlert($message, array $context = array())
  238. {
  239. return $this->addRecord(self::ALERT, $message, $context);
  240. }
  241. /**
  242. * Gets the name of the logging level.
  243. *
  244. * @param integer $level
  245. * @return string
  246. */
  247. public static function getLevelName($level)
  248. {
  249. return self::$levels[$level];
  250. }
  251. // ZF Logger Compat
  252. /**
  253. * Adds a log record at the DEBUG level.
  254. *
  255. * This method allows to have an easy ZF compatibility.
  256. *
  257. * @param string $message The log message
  258. * @param array $context The log context
  259. * @return Boolean Whether the record has been processed
  260. */
  261. public function debug($message, array $context = array())
  262. {
  263. return $this->addRecord(self::DEBUG, $message, $context);
  264. }
  265. /**
  266. * Adds a log record at the INFO level.
  267. *
  268. * This method allows to have an easy ZF compatibility.
  269. *
  270. * @param string $message The log message
  271. * @param array $context The log context
  272. * @return Boolean Whether the record has been processed
  273. */
  274. public function info($message, array $context = array())
  275. {
  276. return $this->addRecord(self::INFO, $message, $context);
  277. }
  278. /**
  279. * Adds a log record at the INFO level.
  280. *
  281. * This method allows to have an easy ZF compatibility.
  282. *
  283. * @param string $message The log message
  284. * @param array $context The log context
  285. * @return Boolean Whether the record has been processed
  286. */
  287. public function notice($message, array $context = array())
  288. {
  289. return $this->addRecord(self::INFO, $message, $context);
  290. }
  291. /**
  292. * Adds a log record at the WARNING level.
  293. *
  294. * This method allows to have an easy ZF compatibility.
  295. *
  296. * @param string $message The log message
  297. * @param array $context The log context
  298. * @return Boolean Whether the record has been processed
  299. */
  300. public function warn($message, array $context = array())
  301. {
  302. return $this->addRecord(self::WARNING, $message, $context);
  303. }
  304. /**
  305. * Adds a log record at the ERROR level.
  306. *
  307. * This method allows to have an easy ZF compatibility.
  308. *
  309. * @param string $message The log message
  310. * @param array $context The log context
  311. * @return Boolean Whether the record has been processed
  312. */
  313. public function err($message, array $context = array())
  314. {
  315. return $this->addRecord(self::ERROR, $message, $context);
  316. }
  317. /**
  318. * Adds a log record at the CRITICAL level.
  319. *
  320. * This method allows to have an easy ZF compatibility.
  321. *
  322. * @param string $message The log message
  323. * @param array $context The log context
  324. * @return Boolean Whether the record has been processed
  325. */
  326. public function crit($message, array $context = array())
  327. {
  328. return $this->addRecord(self::CRITICAL, $message, $context);
  329. }
  330. /**
  331. * Adds a log record at the ALERT level.
  332. *
  333. * This method allows to have an easy ZF compatibility.
  334. *
  335. * @param string $message The log message
  336. * @param array $context The log context
  337. * @return Boolean Whether the record has been processed
  338. */
  339. public function alert($message, array $context = array())
  340. {
  341. return $this->addRecord(self::ALERT, $message, $context);
  342. }
  343. /**
  344. * Adds a log record at the ALERT level.
  345. *
  346. * This method allows to have an easy ZF compatibility.
  347. *
  348. * @param string $message The log message
  349. * @param array $context The log context
  350. * @return Boolean Whether the record has been processed
  351. */
  352. public function emerg($message, array $context = array())
  353. {
  354. return $this->addRecord(self::ALERT, $message, $context);
  355. }
  356. }