AmqpHandler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Formatter\FormatterInterface;
  13. use Monolog\Formatter\JsonFormatter;
  14. use PhpAmqpLib\Message\AMQPMessage;
  15. use PhpAmqpLib\Channel\AMQPChannel;
  16. use AMQPExchange;
  17. use Monolog\LogRecord;
  18. class AmqpHandler extends AbstractProcessingHandler
  19. {
  20. protected AMQPExchange|AMQPChannel $exchange;
  21. /** @var array<string, mixed> */
  22. private array $extraAttributes = [];
  23. protected string $exchangeName;
  24. /**
  25. * @param AMQPExchange|AMQPChannel $exchange AMQPExchange (php AMQP ext) or PHP AMQP lib channel, ready for use
  26. * @param string|null $exchangeName Optional exchange name, for AMQPChannel (PhpAmqpLib) only
  27. */
  28. public function __construct(AMQPExchange|AMQPChannel $exchange, ?string $exchangeName = null, int|string|Level $level = Level::Debug, bool $bubble = true)
  29. {
  30. if ($exchange instanceof AMQPChannel) {
  31. $this->exchangeName = (string) $exchangeName;
  32. } elseif ($exchangeName !== null) {
  33. @trigger_error('The $exchangeName parameter can only be passed when using PhpAmqpLib, if using an AMQPExchange instance configure it beforehand', E_USER_DEPRECATED);
  34. }
  35. $this->exchange = $exchange;
  36. parent::__construct($level, $bubble);
  37. }
  38. /**
  39. * @return array<string, mixed>
  40. */
  41. public function getExtraAttributes(): array
  42. {
  43. return $this->extraAttributes;
  44. }
  45. /**
  46. * Configure extra attributes to pass to the AMQPExchange (if you are using the amqp extension)
  47. *
  48. * @param array<string, mixed> $extraAttributes One of content_type, content_encoding,
  49. * message_id, user_id, app_id, delivery_mode,
  50. * priority, timestamp, expiration, type
  51. * or reply_to, headers.
  52. * @return $this
  53. */
  54. public function setExtraAttributes(array $extraAttributes): self
  55. {
  56. $this->extraAttributes = $extraAttributes;
  57. return $this;
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. protected function write(LogRecord $record): void
  63. {
  64. $data = $record->formatted;
  65. $routingKey = $this->getRoutingKey($record);
  66. if ($this->exchange instanceof AMQPExchange) {
  67. $attributes = [
  68. 'delivery_mode' => 2,
  69. 'content_type' => 'application/json',
  70. ];
  71. if (\count($this->extraAttributes) > 0) {
  72. $attributes = array_merge($attributes, $this->extraAttributes);
  73. }
  74. $this->exchange->publish(
  75. $data,
  76. $routingKey,
  77. 0,
  78. $attributes
  79. );
  80. } else {
  81. $this->exchange->basic_publish(
  82. $this->createAmqpMessage($data),
  83. $this->exchangeName,
  84. $routingKey
  85. );
  86. }
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. public function handleBatch(array $records): void
  92. {
  93. if ($this->exchange instanceof AMQPExchange) {
  94. parent::handleBatch($records);
  95. return;
  96. }
  97. foreach ($records as $record) {
  98. if (!$this->isHandling($record)) {
  99. continue;
  100. }
  101. $record = $this->processRecord($record);
  102. $data = $this->getFormatter()->format($record);
  103. $this->exchange->batch_basic_publish(
  104. $this->createAmqpMessage($data),
  105. $this->exchangeName,
  106. $this->getRoutingKey($record)
  107. );
  108. }
  109. $this->exchange->publish_batch();
  110. }
  111. /**
  112. * Gets the routing key for the AMQP exchange
  113. */
  114. protected function getRoutingKey(LogRecord $record): string
  115. {
  116. $routingKey = sprintf('%s.%s', $record->level->name, $record->channel);
  117. return strtolower($routingKey);
  118. }
  119. private function createAmqpMessage(string $data): AMQPMessage
  120. {
  121. $attributes = [
  122. 'delivery_mode' => 2,
  123. 'content_type' => 'application/json',
  124. ];
  125. if (\count($this->extraAttributes) > 0) {
  126. $attributes = array_merge($attributes, $this->extraAttributes);
  127. }
  128. return new AMQPMessage($data, $attributes);
  129. }
  130. /**
  131. * @inheritDoc
  132. */
  133. protected function getDefaultFormatter(): FormatterInterface
  134. {
  135. return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  136. }
  137. }