Key metrics and engagement data
Repository has been active for N/A
Looks like this repository is a hidden gem!
No stargazers yet. Why not be the first to give it a star?
Check back soon, we will update it in background!
⭐0
Want deeper insights? Explore GitObs.com
This is a fork and continuation of the original maxbanton/cwh repository.
Handler for PHP logging library Monolog for sending log entries to AWS CloudWatch Logs service.
Before using this library, it's recommended to get acquainted with the pricing for AWS CloudWatch services.
Please press ★ Star button if you find this library useful.
This library uses AWS API through AWS PHP SDK, which has limits on concurrent requests. It means that on high concurrent or high load applications it may not work on it's best way. Please consider using another solution such as logging to the stdout and redirecting logs with fluentd.
Install the latest version with Composer by running
bash1$ composer require phpnexus/cwh:^3.0
php1<?php23use Aws\CloudWatchLogs\CloudWatchLogsClient;4use Monolog\Logger;5use Monolog\Level;6use Monolog\Formatter\JsonFormatter;7use PhpNexus\Cwh\Handler\CloudWatch;89$sdkParams = [10 'region' => 'eu-west-1',11 'version' => 'latest',12 'credentials' => [13 'key' => 'your AWS key',14 'secret' => 'your AWS secret',15 'token' => 'your AWS session token', // token is optional16 ]17];1819// Instantiate AWS SDK CloudWatch Logs Client20$client = new CloudWatchLogsClient($sdkParams);2122// Log group name, will be created if none23$groupName = 'php-logtest';2425// Log stream name, will be created if none26$streamName = 'ec2-instance-1';2728// Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.29$retentionDays = 30;3031// Instantiate handler (tags are optional)32$handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, ['my-awesome-tag' => 'tag-value'], Level::Info);3334// Optionally set the JsonFormatter to be able to access your log messages in a structured way35$handler->setFormatter(new JsonFormatter());3637// Create a log channel38$log = new Logger('name');3940// Set handler41$log->pushHandler($handler);4243// Add records to the log44$log->debug('Foo');45$log->warning('Bar');46$log->error('Baz');
The default behavior is to check if the destination log group and log stream exists and create the log group and log stream if necessary.
This activity always sends a DescribeLogGroups
and DescribeLogStreams
API call to AWS, and will send a CreateLogGroup
API call or CreateLogStream
API call to AWS if the log group or log stream doesn't exist.
AWS have a default quota of 10 requests per second for DescribeLogGroups
and 25 requests per second DescribeLogStreams
per region per account, which will become a bottleneck even in medium traffic environments.
By setting $createGroup
and $createStream
to false
, this library will not automatically create the destination log group or log stream, and hence will not send any DescribeLogGroups
or DescribeLogStreams
API calls to AWS.
php1<?php23use Aws\CloudWatchLogs\CloudWatchLogsClient;4use Monolog\Logger;5use Monolog\Level;6use Monolog\Formatter\JsonFormatter;7use PhpNexus\Cwh\Handler\CloudWatch;89$sdkParams = [10 'region' => 'ap-northeast-1',11 'version' => 'latest',12 'credentials' => [13 'key' => 'your AWS key',14 'secret' => 'your AWS secret',15 'token' => 'your AWS session token', // token is optional16 ]17];1819// Instantiate AWS SDK CloudWatch Logs Client20$client = new CloudWatchLogsClient($sdkParams);2122// Log group name (must exist already)23$groupName = 'php-logtest';2425// Log stream name (must exist already)26$streamName = 'ec2-instance-1';2728// Instantiate handler29$handler = new CloudWatch($client, $groupName, $streamName, level: Level::Info, createGroup: false, createStream: false);3031// Optionally set the JsonFormatter to be able to access your log messages in a structured way32$handler->setFormatter(new JsonFormatter());3334// Create a log channel35$log = new Logger('name');3637// Set handler38$log->pushHandler($handler);3940// Add records to the log41$log->debug('Foo');42$log->warning('Bar');43$log->error('Baz');
The default behavior is to send logs in batches of 10000, or when the script terminates. This is appropriate for short-lived requests, but not for long-lived CLI daemons and workers.
For these cases, a smaller $batchSize
of 1 would be more appropriate. However, with a smaller batch size the number of putLogEvents
requests to AWS will increase and may reach the per account per region limit.
To help avoid this rate limit, use the $rpsLimit
option to limit the number of requests per second that your CLI daemon or worker can send.
Note: This limit is only applicable for one instance of a CLI daemon or worker. With multiple instances, adjust the $rpsLimit
accordingly.
php1<?php23use Aws\CloudWatchLogs\CloudWatchLogsClient;4use Monolog\Logger;5use Monolog\Level;6use Monolog\Formatter\JsonFormatter;7use PhpNexus\Cwh\Handler\CloudWatch;89$sdkParams = [10 'region' => 'ap-northeast-1',11 'version' => 'latest',12 'credentials' => [13 'key' => 'your AWS key',14 'secret' => 'your AWS secret',15 'token' => 'your AWS session token', // token is optional16 ]17];1819// Instantiate AWS SDK CloudWatch Logs Client20$client = new CloudWatchLogsClient($sdkParams);2122// Log group name, will be created if none23$groupName = 'php-logtest';2425// Log stream name, will be created if none26$streamName = 'cli-worker';2728// Instantiate handler29$handler = new CloudWatch($client, $groupName, $streamName, batchSize: 1, level: Level::Info, rpsLimit: 100);3031// Optionally set the JsonFormatter to be able to access your log messages in a structured way32$handler->setFormatter(new JsonFormatter());3334// Create a log channel35$log = new Logger('name');3637// Set handler38$log->pushHandler($handler);3940// Add lots of records to the log very quickly41$i = 0;42do {43 $log->info('Foo');44} while ($i++ < 500);
If you prefer to use a separate programmatic IAM user (recommended) or want to define a policy, you will need the following permissions depending on your configuration.
Always required:
PutLogEvents
aws docsIf $createGroup
is true
(default):
If $createStream
is true
(default):
Note: The below samples include permissions to create log groups and streams. Remove the "AllowCreateLogGroup" statement when setting the $createGroup
argument to false
. Remove the "AllowCreateLogStream" statement when setting the $createStream
argument to false
.
This policy example allows writing to any log stream in a log group (named my-app
). The log streams will be created automatically.
json1{2 "Version": "2012-10-17",3 "Statement": [4 {5 "Sid": "AllowCreateLogGroup",6 "Effect": "Allow",7 "Action": [8 "logs:CreateLogGroup",9 "logs:DescribeLogGroups",10 "logs:PutRetentionPolicy"11 ],12 "Resource": "arn:aws:logs:*:*:log-group:*"13 },14 {15 "Sid": "AllowCreateLogStream",16 "Effect": "Allow",17 "Action": [18 "logs:CreateLogStream",19 "logs:DescribeLogStreams"20 ],21 "Resource": "arn:aws:logs:*:*:log-group:*:*"22 },23 {24 "Sid": "AllowPutLogEvents",25 "Effect": "Allow",26 "Action": "logs:PutLogEvents",27 "Resource": "arn:aws:logs:*:*:log-group:*:*"28 }29 ]30}
This policy example allows writing to any log stream in a log group (named my-app
). The log streams will be created automatically.
json1{2 "Version": "2012-10-17",3 "Statement": [4 {5 "Sid": "AllowCreateLogGroup",6 "Effect": "Allow",7 "Action": [8 "logs:CreateLogGroup",9 "logs:DescribeLogGroups",10 "logs:PutRetentionPolicy"11 ],12 "Resource": "arn:aws:logs:*:*:log-group:*"13 },14 {15 "Sid": "AllowCreateLogStream",16 "Effect": "Allow",17 "Action": [18 "logs:CreateLogStream",19 "logs:DescribeLogStreams"20 ],21 "Resource": "arn:aws:logs:*:*:log-group:my-app:*"22 },23 {24 "Sid": "AllowPutLogEvents",25 "Effect": "Allow",26 "Action": "logs:PutLogEvents",27 "Resource": "arn:aws:logs:*:*:log-group:my-app:*"28 }29 ]30}
This policy example allows writing to specific log streams (named my-stream-1
and my-stream-2
) in a log group (named my-app
).
json1{2 "Version": "2012-10-17",3 "Statement": [4 {5 "Sid": "AllowCreateLogGroup",6 "Effect": "Allow",7 "Action": [8 "logs:CreateLogGroup",9 "logs:DescribeLogGroups",10 "logs:PutRetentionPolicy"11 ],12 "Resource": "arn:aws:logs:*:*:log-group:*"13 },14 {15 "Sid": "AllowCreateLogStream",16 "Effect": "Allow",17 "Action": [18 "logs:CreateLogStream",19 "logs:DescribeLogStreams"20 ],21 "Resource": "arn:aws:logs:*:*:log-group:my-app:*"22 },23 {24 "Sid": "AllowPutLogEvents",25 "Effect": "Allow",26 "Action": "logs:PutLogEvents",27 "Resource": [28 "arn:aws:logs:*:*:log-group:my-app:log-stream:my-stream-1",29 "arn:aws:logs:*:*:log-group:my-app:log-stream:my-stream-2",30 ]31 }32 ]33}
Reference: Actions, resources, and condition keys for Amazon CloudWatch Logs
Feel free to report any issues
Please check this document
Made in Ukraine 🇺🇦