mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-02 11:20:31 +02:00
Add the ability to set or redefine attributes for messages in Rabbit. (#1724)
One of content_type, content_encoding, message_id, user_id, app_id, delivery_mode, priority, timestamp, expiration, type or reply_to, headers. Co-authored-by: Попов Михаил <popov.mv@betcity.ru>
This commit is contained in:
@@ -27,6 +27,29 @@ class AmqpHandler extends AbstractProcessingHandler
|
|||||||
* @var AMQPExchange|AMQPChannel $exchange
|
* @var AMQPExchange|AMQPChannel $exchange
|
||||||
*/
|
*/
|
||||||
protected $exchange;
|
protected $exchange;
|
||||||
|
/** @var array */
|
||||||
|
private $extraAttributes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getExtraAttributes(): array
|
||||||
|
{
|
||||||
|
return $this->extraAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $extraAttributes One of content_type, content_encoding,
|
||||||
|
* message_id, user_id, app_id, delivery_mode,
|
||||||
|
* priority, timestamp, expiration, type
|
||||||
|
* or reply_to, headers.
|
||||||
|
* @return AmqpHandler
|
||||||
|
*/
|
||||||
|
public function setExtraAttributes(array $extraAttributes): self
|
||||||
|
{
|
||||||
|
$this->extraAttributes = $extraAttributes;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -60,14 +83,18 @@ class AmqpHandler extends AbstractProcessingHandler
|
|||||||
$routingKey = $this->getRoutingKey($record);
|
$routingKey = $this->getRoutingKey($record);
|
||||||
|
|
||||||
if ($this->exchange instanceof AMQPExchange) {
|
if ($this->exchange instanceof AMQPExchange) {
|
||||||
|
$attributes = [
|
||||||
|
'delivery_mode' => 2,
|
||||||
|
'content_type' => 'application/json',
|
||||||
|
];
|
||||||
|
if ($this->extraAttributes) {
|
||||||
|
$attributes = array_merge($attributes, $this->extraAttributes);
|
||||||
|
}
|
||||||
$this->exchange->publish(
|
$this->exchange->publish(
|
||||||
$data,
|
$data,
|
||||||
$routingKey,
|
$routingKey,
|
||||||
0,
|
0,
|
||||||
[
|
$attributes
|
||||||
'delivery_mode' => 2,
|
|
||||||
'content_type' => 'application/json',
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$this->exchange->basic_publish(
|
$this->exchange->basic_publish(
|
||||||
|
Reference in New Issue
Block a user