FallbackGroupHandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 Throwable;
  12. use Monolog\LogRecord;
  13. /**
  14. * Forwards records to at most one handler
  15. *
  16. * If a handler fails, the exception is suppressed and the record is forwarded to the next handler.
  17. *
  18. * As soon as one handler handles a record successfully, the handling stops there.
  19. */
  20. class FallbackGroupHandler extends GroupHandler
  21. {
  22. /**
  23. * @inheritDoc
  24. */
  25. public function handle(LogRecord $record): bool
  26. {
  27. if (\count($this->processors) > 0) {
  28. $record = $this->processRecord($record);
  29. }
  30. foreach ($this->handlers as $handler) {
  31. try {
  32. $handler->handle(clone $record);
  33. break;
  34. } catch (Throwable $e) {
  35. // What throwable?
  36. }
  37. }
  38. return false === $this->bubble;
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. public function handleBatch(array $records): void
  44. {
  45. if (\count($this->processors) > 0) {
  46. $processed = [];
  47. foreach ($records as $record) {
  48. $processed[] = $this->processRecord($record);
  49. }
  50. $records = $processed;
  51. }
  52. foreach ($this->handlers as $handler) {
  53. try {
  54. $handler->handleBatch(array_map(fn ($record) => clone $record, $records));
  55. break;
  56. } catch (Throwable $e) {
  57. // What throwable?
  58. }
  59. }
  60. }
  61. }