AbstractHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Handler;
  11. use Monolog\Logger;
  12. use Monolog\Formatter\FormatterInterface;
  13. use Monolog\Formatter\LineFormatter;
  14. /**
  15. * Base Handler class providing the Handler structure
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. abstract class AbstractHandler implements HandlerInterface
  20. {
  21. protected $level = Logger::DEBUG;
  22. protected $bubble = true;
  23. /**
  24. * @var FormatterInterface
  25. */
  26. protected $formatter;
  27. protected $processors = array();
  28. /**
  29. * @param int $level The minimum logging level at which this handler will be triggered
  30. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  31. */
  32. public function __construct($level = Logger::DEBUG, $bubble = true)
  33. {
  34. $this->setLevel($level);
  35. $this->bubble = $bubble;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function isHandling(array $record)
  41. {
  42. return $record['level'] >= $this->level;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function handleBatch(array $records)
  48. {
  49. foreach ($records as $record) {
  50. $this->handle($record);
  51. }
  52. }
  53. /**
  54. * Closes the handler.
  55. *
  56. * This will be called automatically when the object is destroyed
  57. */
  58. public function close()
  59. {
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function pushProcessor($callback)
  65. {
  66. if (!is_callable($callback)) {
  67. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
  68. }
  69. array_unshift($this->processors, $callback);
  70. return $this;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function popProcessor()
  76. {
  77. if (!$this->processors) {
  78. throw new \LogicException('You tried to pop from an empty processor stack.');
  79. }
  80. return array_shift($this->processors);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setFormatter(FormatterInterface $formatter)
  86. {
  87. $this->formatter = $formatter;
  88. return $this;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getFormatter()
  94. {
  95. if (!$this->formatter) {
  96. $this->formatter = $this->getDefaultFormatter();
  97. }
  98. return $this->formatter;
  99. }
  100. /**
  101. * Sets minimum logging level at which this handler will be triggered.
  102. *
  103. * @param int $level
  104. * @return self
  105. */
  106. public function setLevel($level)
  107. {
  108. $this->level = Logger::toMonologLevel($level);
  109. return $this;
  110. }
  111. /**
  112. * Gets minimum logging level at which this handler will be triggered.
  113. *
  114. * @return int
  115. */
  116. public function getLevel()
  117. {
  118. return $this->level;
  119. }
  120. /**
  121. * Sets the bubbling behavior.
  122. *
  123. * @param Boolean $bubble true means that this handler allows bubbling.
  124. * false means that bubbling is not permitted.
  125. * @return self
  126. */
  127. public function setBubble($bubble)
  128. {
  129. $this->bubble = $bubble;
  130. return $this;
  131. }
  132. /**
  133. * Gets the bubbling behavior.
  134. *
  135. * @return Boolean true means that this handler allows bubbling.
  136. * false means that bubbling is not permitted.
  137. */
  138. public function getBubble()
  139. {
  140. return $this->bubble;
  141. }
  142. public function __destruct()
  143. {
  144. try {
  145. $this->close();
  146. } catch (\Exception $e) {
  147. // do nothing
  148. }
  149. }
  150. /**
  151. * Gets the default formatter.
  152. *
  153. * @return FormatterInterface
  154. */
  155. protected function getDefaultFormatter()
  156. {
  157. return new LineFormatter();
  158. }
  159. }