AbstractHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = false;
  23. /**
  24. * @var FormatterInterface
  25. */
  26. protected $formatter;
  27. protected $processors = array();
  28. /**
  29. * @param integer $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->level = $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. }
  71. /**
  72. * {@inheritdoc}
  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. /**
  82. * {@inheritdoc}
  83. */
  84. public function setFormatter(FormatterInterface $formatter)
  85. {
  86. $this->formatter = $formatter;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getFormatter()
  92. {
  93. if (!$this->formatter) {
  94. $this->formatter = $this->getDefaultFormatter();
  95. }
  96. return $this->formatter;
  97. }
  98. /**
  99. * Sets minimum logging level at which this handler will be triggered.
  100. *
  101. * @param integer $level
  102. */
  103. public function setLevel($level)
  104. {
  105. $this->level = $level;
  106. }
  107. /**
  108. * Gets minimum logging level at which this handler will be triggered.
  109. *
  110. * @return integer
  111. */
  112. public function getLevel()
  113. {
  114. return $this->level;
  115. }
  116. /**
  117. * Sets the bubbling behavior.
  118. *
  119. * @param Boolean $bubble True means that bubbling is not permitted.
  120. * False means that this handler allows bubbling.
  121. */
  122. public function setBubble($bubble)
  123. {
  124. $this->bubble = $bubble;
  125. }
  126. /**
  127. * Gets the bubbling behavior.
  128. *
  129. * @return Boolean True means that bubbling is not permitted.
  130. * False means that this handler allows bubbling.
  131. */
  132. public function getBubble()
  133. {
  134. return $this->bubble;
  135. }
  136. public function __destruct()
  137. {
  138. $this->close();
  139. }
  140. /**
  141. * Gets the default formatter.
  142. *
  143. * @return FormatterInterface
  144. */
  145. protected function getDefaultFormatter()
  146. {
  147. return new LineFormatter();
  148. }
  149. }