BufferHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php declare(strict_types=1);
  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\ResettableInterface;
  13. use Monolog\Formatter\FormatterInterface;
  14. /**
  15. * Buffers all records until closing the handler and then pass them as batch.
  16. *
  17. * This is useful for a MailHandler to send only one mail per request instead of
  18. * sending one per log message.
  19. *
  20. * @author Christophe Coevoet <stof@notk.org>
  21. */
  22. class BufferHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
  23. {
  24. use ProcessableHandlerTrait;
  25. protected $handler;
  26. protected $bufferSize = 0;
  27. protected $bufferLimit;
  28. protected $flushOnOverflow;
  29. protected $buffer = [];
  30. protected $initialized = false;
  31. /**
  32. * @param HandlerInterface $handler Handler.
  33. * @param int $bufferLimit How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
  34. * @param string|int $level The minimum logging level at which this handler will be triggered
  35. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  36. * @param bool $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded
  37. */
  38. public function __construct(HandlerInterface $handler, int $bufferLimit = 0, $level = Logger::DEBUG, bool $bubble = true, bool $flushOnOverflow = false)
  39. {
  40. parent::__construct($level, $bubble);
  41. $this->handler = $handler;
  42. $this->bufferLimit = $bufferLimit;
  43. $this->flushOnOverflow = $flushOnOverflow;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function handle(array $record): bool
  49. {
  50. if ($record['level'] < $this->level) {
  51. return false;
  52. }
  53. if (!$this->initialized) {
  54. // __destructor() doesn't get called on Fatal errors
  55. register_shutdown_function([$this, 'close']);
  56. $this->initialized = true;
  57. }
  58. if ($this->bufferLimit > 0 && $this->bufferSize === $this->bufferLimit) {
  59. if ($this->flushOnOverflow) {
  60. $this->flush();
  61. } else {
  62. array_shift($this->buffer);
  63. $this->bufferSize--;
  64. }
  65. }
  66. if ($this->processors) {
  67. $record = $this->processRecord($record);
  68. }
  69. $this->buffer[] = $record;
  70. $this->bufferSize++;
  71. return false === $this->bubble;
  72. }
  73. public function flush(): void
  74. {
  75. if ($this->bufferSize === 0) {
  76. return;
  77. }
  78. $this->handler->handleBatch($this->buffer);
  79. $this->clear();
  80. }
  81. public function __destruct()
  82. {
  83. // suppress the parent behavior since we already have register_shutdown_function()
  84. // to call close(), and the reference contained there will prevent this from being
  85. // GC'd until the end of the request
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function close(): void
  91. {
  92. $this->flush();
  93. $this->handler->close();
  94. }
  95. /**
  96. * Clears the buffer without flushing any messages down to the wrapped handler.
  97. */
  98. public function clear(): void
  99. {
  100. $this->bufferSize = 0;
  101. $this->buffer = [];
  102. }
  103. public function reset()
  104. {
  105. $this->flush();
  106. parent::reset();
  107. $this->resetProcessors();
  108. if ($this->handler instanceof ResettableInterface) {
  109. $this->handler->reset();
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  116. {
  117. $this->handler->setFormatter($formatter);
  118. return $this;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getFormatter(): FormatterInterface
  124. {
  125. return $this->handler->getFormatter();
  126. }
  127. }