diff --git a/composer.json b/composer.json index a6f4ea8..6f6f10a 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "require": { "php": "^7.2 || ^8", - "monolog/monolog": "^2.0", + "monolog/monolog": "^2.0 || ^3.0", "aws/aws-sdk-php": "^3.18" }, "require-dev": { diff --git a/src/Handler/CloudWatch.php b/src/Handler/CloudWatch.php index 26a1271..85c2196 100755 --- a/src/Handler/CloudWatch.php +++ b/src/Handler/CloudWatch.php @@ -161,7 +161,7 @@ public function __construct( /** * {@inheritdoc} */ - protected function write(array $record): void + protected function write(\Monolog\LogRecord $record): void { $records = $this->formatRecords($record); @@ -280,10 +280,22 @@ protected function willMessageTimestampExceedLimit(array $record): bool * @param array $entry * @return array */ - private function formatRecords(array $entry): array + private function formatRecords(\Monolog\LogRecord $entry): array { - $entries = str_split($entry['formatted'], self::EVENT_SIZE_LIMIT); - $timestamp = $entry['datetime']->format('U.u') * 1000; + // Extract the formatted message from the LogRecord + $formattedMessage = $entry->formatted; + + // If formattedMessage is not yet defined, format it here (depends on how your formatter works) + if (is_null($formattedMessage)) { + $formattedMessage = sprintf("[%s] %s: %s", $entry->datetime->format('Y-m-d H:i:s'), $entry->level->name, $entry->message); + if (!empty($entry->context)) { + $formattedMessage .= ' ' . json_encode($entry->context); + } + } + + // Split the message into chunks based on EVENT_SIZE_LIMIT + $entries = str_split($formattedMessage, self::EVENT_SIZE_LIMIT); + $timestamp = $entry->datetime->format('U.u') * 1000; $records = []; foreach ($entries as $entry) {