Skip to content

Commit 84d1cd2

Browse files
soufianshiawaSoufianLajderusseasync-aws-bot
authored
feat: add support for monolog 3 (#1638)
* feat: add support for monolog 3/ symfony 7 * fix: keep the support for php7 * fix: handle cases for php8/7 * Prepare new release (#1640) * Bump internal dependencies (#1641) * Bump packages to version 1.0 (#1649) * Update generated code (#1652) update generated code * Update generated code (#1654) update generated code * fix: phpStan issues * fix: doc GeoProximityLocation * fix: issue psalm/CsFixer * fix: generated output * Fix typehint for getMessageSize --------- Co-authored-by: soufianlagnaoui <[email protected]> Co-authored-by: Jérémy Derussé <[email protected]> Co-authored-by: AsyncAws <[email protected]>
1 parent cc96ae4 commit 84d1cd2

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"illuminate/queue": "^6.18.11 || ^7.10 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
2323
"illuminate/support": "^6.18.13 || ^7.10 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
2424
"matthiasnoback/symfony-config-test": "^4.1 || ^5.0",
25-
"monolog/monolog": "^1.1 || ^2.0",
25+
"monolog/monolog": "^1.1 || ^2.0 || ^3.0",
2626
"nette/php-generator": "^3.6 || ^4.1",
2727
"nette/utils": "^3.0 || ^4.0",
2828
"nikic/php-parser": "^4.0",

src/Integration/Monolog/CloudWatch/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## NOT RELEASED
44

5+
### Added
6+
7+
- Added support for `monolog/monolog` 3.0
8+
59
## 1.1.0
610

711
### Added

src/Integration/Monolog/CloudWatch/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"require": {
1414
"php": "^7.2.5 || ^8.0",
1515
"async-aws/cloud-watch-logs": "^1.0 || ^2.0",
16-
"monolog/monolog": "^1.1 || ^2.0"
16+
"monolog/monolog": "^1.1 || ^2.0 || ^3.0"
1717
},
1818
"autoload": {
1919
"psr-4": {
@@ -27,7 +27,7 @@
2727
},
2828
"extra": {
2929
"branch-alias": {
30-
"dev-master": "1.1-dev"
30+
"dev-master": "1.2-dev"
3131
}
3232
}
3333
}

src/Integration/Monolog/CloudWatch/src/CloudWatchLogsHandler.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
use Monolog\Formatter\LineFormatter;
1111
use Monolog\Handler\AbstractProcessingHandler;
1212
use Monolog\Logger;
13+
use Monolog\LogRecord;
1314

14-
/**
15-
* @phpstan-import-type FormattedRecord from AbstractProcessingHandler
16-
*/
1715
class CloudWatchLogsHandler extends AbstractProcessingHandler
1816
{
1917
/**
@@ -126,20 +124,21 @@ protected function getDefaultFormatter(): FormatterInterface
126124
}
127125

128126
/**
129-
* {@inheritdoc}
127+
* @param LogRecord|array $record
130128
*/
131-
protected function write(array $record): void
129+
protected function write($record): void
132130
{
133-
$records = $this->formatRecords($record);
131+
$entries = $this->formatRecords($record);
134132

135-
foreach ($records as $record) {
136-
if ($this->currentDataAmount + $this->getMessageSize($record) >= self::DATA_AMOUNT_LIMIT) {
133+
foreach ($entries as $entry) {
134+
$entrySize = $this->getEntrySize($entry);
135+
if ($this->currentDataAmount + $entrySize >= self::DATA_AMOUNT_LIMIT) {
137136
$this->flushBuffer();
138137
}
139138

140-
$this->currentDataAmount += $this->getMessageSize($record);
139+
$this->currentDataAmount += $entrySize;
141140

142-
$this->buffer[] = $record;
141+
$this->buffer[] = $entry;
143142

144143
if (\count($this->buffer) >= $this->options['batchSize']) {
145144
$this->flushBuffer();
@@ -186,14 +185,18 @@ private function flushBuffer(): void
186185
* Event size in the batch can not be bigger than 256 KB
187186
* https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html.
188187
*
189-
* @phpstan-param FormattedRecord $entry
188+
* @param LogRecord|array $record
190189
*
191190
* @return list<array{message: string, timestamp: int|float}>
192191
*/
193-
private function formatRecords(array $entry): array
192+
private function formatRecords($record): array
194193
{
195-
$entries = str_split($entry['formatted'], self::EVENT_SIZE_LIMIT);
196-
$timestamp = $entry['datetime']->format('U.u') * 1000;
194+
if (!$record instanceof LogRecord && !\is_array($record)) {
195+
throw new InvalidArgument(sprintf('Argument 1 passed to %s must be of the type array or LogRecord, %s given', __METHOD__, \gettype($record)));
196+
}
197+
198+
$entries = str_split($record['formatted'], self::EVENT_SIZE_LIMIT);
199+
$timestamp = $record['datetime']->format('U.u') * 1000;
197200
$records = [];
198201

199202
foreach ($entries as $entry) {
@@ -209,11 +212,11 @@ private function formatRecords(array $entry): array
209212
/**
210213
* http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html.
211214
*
212-
* @param array{message: string, timestamp: int|float} $record
215+
* @param array{message: string, timestamp: int|float} $entry
213216
*/
214-
private function getMessageSize(array $record): int
217+
private function getEntrySize(array $entry): int
215218
{
216-
return \strlen($record['message']) + 26;
219+
return \strlen($entry['message']) + 26;
217220
}
218221

219222
/**

src/Integration/Monolog/CloudWatch/tests/CloudWatchLogsHandlerTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use AsyncAws\Core\Test\ResultMockFactory;
88
use AsyncAws\Monolog\CloudWatch\CloudWatchLogsHandler;
99
use Monolog\Formatter\LineFormatter;
10+
use Monolog\Level;
1011
use Monolog\Logger;
12+
use Monolog\LogRecord;
1113
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
1315

@@ -43,8 +45,8 @@ public function testConfig()
4345
'group' => $this->groupName,
4446
'stream' => $this->streamName,
4547
], Logger::CRITICAL, false);
46-
47-
self::assertEquals(Logger::CRITICAL, $handler->getLevel());
48+
$level = !\is_int($handler->getLevel()) ? $handler->getLevel()->value : $handler->getLevel();
49+
self::assertEquals(Logger::CRITICAL, $level);
4850
self::assertFalse($handler->getBubble());
4951
}
5052

@@ -115,8 +117,7 @@ public function testSortsEntriesChronologically()
115117
$records = [];
116118

117119
for ($i = 1; $i <= 4; ++$i) {
118-
$record = $this->getRecord(Logger::INFO, 'record' . $i);
119-
$record['datetime'] = \DateTime::createFromFormat('U', time() + $i);
120+
$record = $this->getRecord(Logger::INFO, 'record' . $i, [], \DateTimeImmutable::createFromFormat('U', time() + $i));
120121
$records[] = $record;
121122
}
122123

@@ -138,17 +139,31 @@ private function getHandler($batchSize = 1000): CloudWatchLogsHandler
138139
]);
139140
}
140141

141-
private function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = []): array
142+
private function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = [], ?\DateTimeImmutable $datetime = null)
142143
{
143-
return [
144+
$data = [
144145
'message' => $message,
145146
'context' => $context,
146147
'level' => $level,
147-
'level_name' => Logger::getLevelName($level),
148148
'channel' => 'test',
149-
'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
149+
'datetime' => $datetime ?? \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
150150
'extra' => [],
151151
];
152+
153+
if (!class_exists(LogRecord::class)) {
154+
$data['level_name'] = Logger::getLevelName($data['level']);
155+
156+
return $data;
157+
}
158+
159+
return new LogRecord(
160+
$data['datetime'],
161+
$data['channel'],
162+
Level::fromValue($data['level']),
163+
$data['message'],
164+
$data['context'],
165+
$data['extra']
166+
);
152167
}
153168

154169
private function getMultipleRecords(): array

src/Service/MediaConvert/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
- AWS api-change: This release includes support for bringing your own fonts to use for burn-in or DVB-Sub captioning workflows.
88
- AWS api-change: Change endpoint for `cn-northwest-1` region
99

10-
1110
## 1.1.0
1211

1312
### Added

0 commit comments

Comments
 (0)