Logger.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. {
  85. return $this->name;
  86. }
  87. /**
  88. * Pushes an handler on the stack.
  89. *
  90. * @param HandlerInterface $handler
  91. */
  92. public function pushHandler(HandlerInterface $handler)
  93. {
  94. array_unshift($this->handlers, $handler);
  95. }
  96. /**
  97. * Pops an handler from the stack
  98. *
  99. * @return HandlerInterface
  100. */
  101. public function popHandler()
  102. {
  103. if (!$this->handlers) {
  104. throw new \LogicException('You tried to pop from an empty handler stack.');
  105. }
  106. return array_shift($this->handlers);
  107. }
  108. /**
  109. * Adds a processor in the stack.
  110. *
  111. * @param callable $callback
  112. */
  113. public function pushProcessor($callback)
  114. {
  115. if (!is_callable($callback)) {
  116. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
  117. }
  118. array_unshift($this->processors, $callback);
  119. }
  120. /**
  121. * Removes the processor on top of the stack and returns it.
  122. *
  123. * @return callable
  124. */
  125. public function popProcessor()
  126. {
  127. if (!$this->processors) {
  128. throw new \LogicException('You tried to pop from an empty processor stack.');
  129. }
  130. return array_shift($this->processors);
  131. }
  132. /**
  133. * Adds a log record.
  134. *
  135. * @param integer $level The logging level
  136. * @param string $message The log message
  137. * @param array $context The log context
  138. * @return Boolean Whether the record has been processed
  139. */
  140. public function addRecord($level, $message, array $context = array())
  141. {
  142. if (!$this->handlers) {
  143. $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
  144. }
  145. $record = array(
  146. 'message' => (string) $message,
  147. 'context' => $context,
  148. 'level' => $level,
  149. 'level_name' => self::getLevelName($level),
  150. 'channel' => $this->name,
  151. 'datetime' => new \DateTime(),
  152. 'extra' => array(),
  153. );
  154. // check if any message will handle this message
  155. $handlerKey = null;
  156. foreach ($this->handlers as $key => $handler) {
  157. if ($handler->isHandling($record)) {
  158. $handlerKey = $key;
  159. break;
  160. }
  161. }
  162. // none found
  163. if (null === $handlerKey) {
  164. return false;
  165. }
  166. // found at least one, process message and dispatch it
  167. foreach ($this->processors as $processor) {
  168. $record = call_user_func($processor, $record);
  169. }
  170. while (isset($this->handlers[$handlerKey]) &&
  171. false === $this->handlers[$handlerKey]->handle($record)) {
  172. $handlerKey++;
  173. }
  174. return true;
  175. }
  176. /**
  177. * Adds a log record at the DEBUG level.
  178. *
  179. * @param string $message The log message
  180. * @param array $context The log context
  181. * @return Boolean Whether the record has been processed
  182. */
  183. public function addDebug($message, array $context = array())
  184. {
  185. return $this->addRecord(self::DEBUG, $message, $context);
  186. }
  187. /**
  188. * Adds a log record at the INFO level.
  189. *
  190. * @param string $message The log message
  191. * @param array $context The log context
  192. * @return Boolean Whether the record has been processed
  193. */
  194. public function addInfo($message, array $context = array())
  195. {
  196. return $this->addRecord(self::INFO, $message, $context);
  197. }
  198. /**
  199. * Adds a log record at the WARNING level.
  200. *
  201. * @param string $message The log message
  202. * @param array $context The log context
  203. * @return Boolean Whether the record has been processed
  204. */
  205. public function addWarning($message, array $context = array())
  206. {
  207. return $this->addRecord(self::WARNING, $message, $context);
  208. }
  209. /**
  210. * Adds a log record at the ERROR level.
  211. *
  212. * @param string $message The log message
  213. * @param array $context The log context
  214. * @return Boolean Whether the record has been processed
  215. */
  216. public function addError($message, array $context = array())
  217. {
  218. return $this->addRecord(self::ERROR, $message, $context);
  219. }
  220. /**
  221. * Adds a log record at the CRITICAL level.
  222. *
  223. * @param string $message The log message
  224. * @param array $context The log context
  225. * @return Boolean Whether the record has been processed
  226. */
  227. public function addCritical($message, array $context = array())
  228. {
  229. return $this->addRecord(self::CRITICAL, $message, $context);
  230. }
  231. /**
  232. * Adds a log record at the ALERT level.
  233. *
  234. * @param string $message The log message
  235. * @param array $context The log context
  236. * @return Boolean Whether the record has been processed
  237. */
  238. public function addAlert($message, array $context = array())
  239. {
  240. return $this->addRecord(self::ALERT, $message, $context);
  241. }
  242. /**
  243. * Gets the name of the logging level.
  244. *
  245. * @param integer $level
  246. * @return string
  247. */
  248. public static function getLevelName($level)
  249. {
  250. return self::$levels[$level];
  251. }
  252. // ZF Logger Compat
  253. /**
  254. * Adds a log record at the DEBUG level.
  255. *
  256. * This method allows to have an easy ZF compatibility.
  257. *
  258. * @param string $message The log message
  259. * @param array $context The log context
  260. * @return Boolean Whether the record has been processed
  261. */
  262. public function debug($message, array $context = array())
  263. {
  264. return $this->addRecord(self::DEBUG, $message, $context);
  265. }
  266. /**
  267. * Adds a log record at the INFO level.
  268. *
  269. * This method allows to have an easy ZF compatibility.
  270. *
  271. * @param string $message The log message
  272. * @param array $context The log context
  273. * @return Boolean Whether the record has been processed
  274. */
  275. public function info($message, array $context = array())
  276. {
  277. return $this->addRecord(self::INFO, $message, $context);
  278. }
  279. /**
  280. * Adds a log record at the INFO level.
  281. *
  282. * This method allows to have an easy ZF compatibility.
  283. *
  284. * @param string $message The log message
  285. * @param array $context The log context
  286. * @return Boolean Whether the record has been processed
  287. */
  288. public function notice($message, array $context = array())
  289. {
  290. return $this->addRecord(self::INFO, $message, $context);
  291. }
  292. /**
  293. * Adds a log record at the WARNING level.
  294. *
  295. * This method allows to have an easy ZF compatibility.
  296. *
  297. * @param string $message The log message
  298. * @param array $context The log context
  299. * @return Boolean Whether the record has been processed
  300. */
  301. public function warn($message, array $context = array())
  302. {
  303. return $this->addRecord(self::WARNING, $message, $context);
  304. }
  305. /**
  306. * Adds a log record at the ERROR level.
  307. *
  308. * This method allows to have an easy ZF compatibility.
  309. *
  310. * @param string $message The log message
  311. * @param array $context The log context
  312. * @return Boolean Whether the record has been processed
  313. */
  314. public function err($message, array $context = array())
  315. {
  316. return $this->addRecord(self::ERROR, $message, $context);
  317. }
  318. /**
  319. * Adds a log record at the CRITICAL level.
  320. *
  321. * This method allows to have an easy ZF compatibility.
  322. *
  323. * @param string $message The log message
  324. * @param array $context The log context
  325. * @return Boolean Whether the record has been processed
  326. */
  327. public function crit($message, array $context = array())
  328. {
  329. return $this->addRecord(self::CRITICAL, $message, $context);
  330. }
  331. /**
  332. * Adds a log record at the ALERT level.
  333. *
  334. * This method allows to have an easy ZF compatibility.
  335. *
  336. * @param string $message The log message
  337. * @param array $context The log context
  338. * @return Boolean Whether the record has been processed
  339. */
  340. public function alert($message, array $context = array())
  341. {
  342. return $this->addRecord(self::ALERT, $message, $context);
  343. }
  344. /**
  345. * Adds a log record at the ALERT level.
  346. *
  347. * This method allows to have an easy ZF compatibility.
  348. *
  349. * @param string $message The log message
  350. * @param array $context The log context
  351. * @return Boolean Whether the record has been processed
  352. */
  353. public function emerg($message, array $context = array())
  354. {
  355. return $this->addRecord(self::ALERT, $message, $context);
  356. }
  357. }