BufferHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Level;
  12. use Monolog\ResettableInterface;
  13. use Monolog\Formatter\FormatterInterface;
  14. use Monolog\LogRecord;
  15. /**
  16. * Buffers all records until closing the handler and then pass them as batch.
  17. *
  18. * This is useful for a MailHandler to send only one mail per request instead of
  19. * sending one per log message.
  20. *
  21. * @author Christophe Coevoet <stof@notk.org>
  22. */
  23. class BufferHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
  24. {
  25. use ProcessableHandlerTrait;
  26. protected HandlerInterface $handler;
  27. protected int $bufferSize = 0;
  28. protected int $bufferLimit;
  29. protected bool $flushOnOverflow;
  30. /** @var LogRecord[] */
  31. protected array $buffer = [];
  32. protected bool $initialized = false;
  33. /**
  34. * @param HandlerInterface $handler Handler.
  35. * @param int $bufferLimit How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
  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, int|string|Level $level = Level::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(LogRecord $record): bool
  49. {
  50. if ($record->level->isLowerThan($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 (\count($this->processors) > 0) {
  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(): void
  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. if ($this->handler instanceof FormattableHandlerInterface) {
  118. $this->handler->setFormatter($formatter);
  119. return $this;
  120. }
  121. throw new \UnexpectedValueException('The nested handler of type '.\get_class($this->handler).' does not support formatters.');
  122. }
  123. /**
  124. * @inheritDoc
  125. */
  126. public function getFormatter(): FormatterInterface
  127. {
  128. if ($this->handler instanceof FormattableHandlerInterface) {
  129. return $this->handler->getFormatter();
  130. }
  131. throw new \UnexpectedValueException('The nested handler of type '.\get_class($this->handler).' does not support formatters.');
  132. }
  133. public function setHandler(HandlerInterface $handler): void
  134. {
  135. $this->handler = $handler;
  136. }
  137. }