| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Monolog package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Monolog\Handler;
|
|
|
13 |
|
|
|
14 |
use Aws\Sqs\SqsClient;
|
|
|
15 |
use Monolog\Logger;
|
|
|
16 |
use Monolog\Utils;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Writes to any sqs queue.
|
|
|
20 |
*
|
|
|
21 |
* @author Martijn van Calker <git@amvc.nl>
|
|
|
22 |
*/
|
|
|
23 |
class SqsHandler extends AbstractProcessingHandler
|
|
|
24 |
{
|
|
|
25 |
/** 256 KB in bytes - maximum message size in SQS */
|
|
|
26 |
protected const MAX_MESSAGE_SIZE = 262144;
|
|
|
27 |
/** 100 KB in bytes - head message size for new error log */
|
|
|
28 |
protected const HEAD_MESSAGE_SIZE = 102400;
|
|
|
29 |
|
|
|
30 |
/** @var SqsClient */
|
|
|
31 |
private $client;
|
|
|
32 |
/** @var string */
|
|
|
33 |
private $queueUrl;
|
|
|
34 |
|
|
|
35 |
public function __construct(SqsClient $sqsClient, string $queueUrl, $level = Logger::DEBUG, bool $bubble = true)
|
|
|
36 |
{
|
|
|
37 |
parent::__construct($level, $bubble);
|
|
|
38 |
|
|
|
39 |
$this->client = $sqsClient;
|
|
|
40 |
$this->queueUrl = $queueUrl;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* {@inheritDoc}
|
|
|
45 |
*/
|
|
|
46 |
protected function write(array $record): void
|
|
|
47 |
{
|
|
|
48 |
if (!isset($record['formatted']) || 'string' !== gettype($record['formatted'])) {
|
|
|
49 |
throw new \InvalidArgumentException('SqsHandler accepts only formatted records as a string' . Utils::getRecordMessageForException($record));
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$messageBody = $record['formatted'];
|
|
|
53 |
if (strlen($messageBody) >= static::MAX_MESSAGE_SIZE) {
|
|
|
54 |
$messageBody = Utils::substr($messageBody, 0, static::HEAD_MESSAGE_SIZE);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
$this->client->sendMessage([
|
|
|
58 |
'QueueUrl' => $this->queueUrl,
|
|
|
59 |
'MessageBody' => $messageBody,
|
|
|
60 |
]);
|
|
|
61 |
}
|
|
|
62 |
}
|