Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "maxbanton/cwh",
"name": "vnrag/px-cwh",
"homepage": "https://github.com/maxbanton/cwh",
"type": "library",
"description": "AWS CloudWatch Handler for Monolog library",
Expand All @@ -22,11 +22,11 @@
},
"require": {
"php": "^7.2 || ^8",
"monolog/monolog": "^2.0",
"monolog/monolog": "^3.3.1",
"aws/aws-sdk-php": "^3.18"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.4",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.5"
},
"suggest": {
Expand Down
14 changes: 5 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<coverage>
stopOnFailure="false"
failOnDeprecation="false">
<source>
<include>
<directory>src</directory>
</include>
</coverage>
</source>
<testsuites>
<testsuite name="Handler Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
81 changes: 14 additions & 67 deletions src/Handler/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;

class CloudWatch extends AbstractProcessingHandler
{
Expand Down Expand Up @@ -74,10 +75,6 @@ class CloudWatch extends AbstractProcessingHandler
*/
private $tags = [];

/**
* @var bool
*/
private $createGroup;

/**
* Data amount limit (http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html)
Expand Down Expand Up @@ -124,22 +121,20 @@ class CloudWatch extends AbstractProcessingHandler
* @param int $retention
* @param int $batchSize
* @param array $tags
* @param int $level
* @param Level $level
* @param bool $bubble
* @param bool $createGroup
*
* @throws \Exception
*/
public function __construct(
CloudWatchLogsClient $client,
$group,
$stream,
$retention = 14,
$batchSize = 10000,
string $group,
string $stream,
int $retention = 14,
int $batchSize = 10000,
array $tags = [],
$level = Logger::DEBUG,
$bubble = true,
$createGroup = true
Level $level = Level::Debug,
bool $bubble = true
) {
if ($batchSize > 10000) {
throw new \InvalidArgumentException('Batch size can not be greater than 10000');
Expand All @@ -151,7 +146,6 @@ public function __construct(
$this->retention = $retention;
$this->batchSize = $batchSize;
$this->tags = $tags;
$this->createGroup = $createGroup;

parent::__construct($level, $bubble);

Expand All @@ -161,7 +155,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
protected function write(array $record): void
protected function write(LogRecord $record): void
{
$records = $this->formatRecords($record);

Expand Down Expand Up @@ -277,13 +271,13 @@ protected function willMessageTimestampExceedLimit(array $record): bool
* Event size in the batch can not be bigger than 256 KB
* https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html
*
* @param array $entry
* @param LogRecord $entry
* @return array
*/
private function formatRecords(array $entry): array
private function formatRecords(LogRecord $entry): array
{
$entries = str_split($entry['formatted'], self::EVENT_SIZE_LIMIT);
$timestamp = $entry['datetime']->format('U.u') * 1000;
$entries = str_split($entry->formatted, self::EVENT_SIZE_LIMIT);
$timestamp = $entry->datetime->format('U.u') * 1000;
$records = [];

foreach ($entries as $entry) {
Expand Down Expand Up @@ -324,7 +318,6 @@ private function send(array $entries): void

return 0;
});

$data = [
'logGroupName' => $this->group,
'logStreamName' => $this->stream,
Expand All @@ -342,54 +335,8 @@ private function send(array $entries): void
$this->sequenceToken = $response->get('nextSequenceToken');
}

private function initializeGroup(): void
{
// fetch existing groups
$existingGroups =
$this
->client
->describeLogGroups(['logGroupNamePrefix' => $this->group])
->get('logGroups');

// extract existing groups names
$existingGroupsNames = array_map(
function ($group) {
return $group['logGroupName'];
},
$existingGroups
);

// create group and set retention policy if not created yet
if (!in_array($this->group, $existingGroupsNames, true)) {
$createLogGroupArguments = ['logGroupName' => $this->group];

if (!empty($this->tags)) {
$createLogGroupArguments['tags'] = $this->tags;
}

$this
->client
->createLogGroup($createLogGroupArguments);

if ($this->retention !== null) {
$this
->client
->putRetentionPolicy(
[
'logGroupName' => $this->group,
'retentionInDays' => $this->retention,
]
);
}
}
}

private function initialize(): void
{
if ($this->createGroup) {
$this->initializeGroup();
}

$this->refreshSequenceToken();
}

Expand Down
Loading