GroupHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Formatter\FormatterInterface;
  12. use Monolog\ResettableInterface;
  13. /**
  14. * Forwards records to multiple handlers
  15. *
  16. * @author Lenar Lõhmus <lenar@city.ee>
  17. */
  18. class GroupHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface
  19. {
  20. use ProcessableHandlerTrait;
  21. protected $handlers;
  22. protected $bubble;
  23. /**
  24. * @param HandlerInterface[] $handlers Array of Handlers.
  25. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  26. */
  27. public function __construct(array $handlers, bool $bubble = true)
  28. {
  29. foreach ($handlers as $handler) {
  30. if (!$handler instanceof HandlerInterface) {
  31. throw new \InvalidArgumentException('The first argument of the GroupHandler must be an array of HandlerInterface instances.');
  32. }
  33. }
  34. $this->handlers = $handlers;
  35. $this->bubble = $bubble;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function isHandling(array $record): bool
  41. {
  42. foreach ($this->handlers as $handler) {
  43. if ($handler->isHandling($record)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function handle(array $record): bool
  53. {
  54. if ($this->processors) {
  55. $record = $this->processRecord($record);
  56. }
  57. foreach ($this->handlers as $handler) {
  58. $handler->handle($record);
  59. }
  60. return false === $this->bubble;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function handleBatch(array $records): void
  66. {
  67. if ($this->processors) {
  68. $processed = [];
  69. foreach ($records as $record) {
  70. foreach ($this->processors as $processor) {
  71. $record = call_user_func($processor, $record);
  72. }
  73. $processed[] = $record;
  74. }
  75. $records = $processed;
  76. }
  77. foreach ($this->handlers as $handler) {
  78. $handler->handleBatch($records);
  79. }
  80. }
  81. public function reset()
  82. {
  83. $this->resetProcessors();
  84. foreach ($this->handlers as $handler) {
  85. if ($handler instanceof ResettableInterface) {
  86. $handler->reset();
  87. }
  88. }
  89. }
  90. public function close(): void
  91. {
  92. parent::close();
  93. foreach ($this->handlers as $handler) {
  94. $handler->close();
  95. }
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  101. {
  102. foreach ($this->handlers as $handler) {
  103. $handler->setFormatter($formatter);
  104. }
  105. return $this;
  106. }
  107. }