dhs/cloudwatch-laravel-log
Composer 安装命令:
composer require dhs/cloudwatch-laravel-log
包简介
Push logs to cloudwatch for Laravel >= 9
关键字:
README 文档
README
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.
Requirements
- PHP >=8.1
- AWS account with proper permissions (see list of permissions below)
Features
- Up to 10000 batch logs sending in order to avoid Rate exceeded errors
- Log Groups creating with tags
- AWS CloudWatch Logs staff lazy loading
- Suitable for web applications and for long-living CLI daemons and workers
Installation
Install the latest version with Composer by running
$ composer require dhs/cloudwatch-laravel-log:dev-master
Basic Laravel Usage
- Create a folder Logging and create a file CloudWatchLoggerFactory.php
- Guide Link: Guid
<?php namespace App\Logging; use Aws\CloudWatchLogs\CloudWatchLogsClient; use Dhs\CloudWatchLogs\Handler\CloudWatch; use Monolog\Logger; use Monolog\Formatter\JsonFormatter; use Monolog\Level; class CloudWatchLoggerFactory { /** * Create a custom Monolog instance. * * @param array $config * @return \Monolog\Logger */ public function __invoke(array $config) { // Instantiate AWS SDK CloudWatch Logs Client $client = new CloudWatchLogsClient($config['sdk']); // Instantiate handler (tags are optional) $handler = new CloudWatch( $client, $config['group_name'], $config['stream_name'], $config['retention'], 10000, ['my-awesome-tag' => 'tag-value'], Level::Info ); // Optionally set the JsonFormatter to be able to access your log messages in a structured way $handler->setFormatter(new JsonFormatter()); $name = $config['name'] ?? 'cloudwatch'; // Create a log channel $logger = new Logger($name); // Set handler $logger->pushHandler($handler); return $logger; } }
Config Usage
- open file logging.php in folder config in laravel app and add the code config below
<?php return [ //... 'channels' => [ //.... 'cloudwatch' => [ 'driver' => 'custom', 'via' => \App\Logging\CloudWatchLoggerFactory::class, 'sdk' => [ 'region' => env('AWS_DEFAULT_REGION', 'eu-west-1'), 'version' => 'latest', 'credentials' => [ 'key' => env('AWS_ACCESS_KEY_ID', ''), 'secret' => env('AWS_SECRET_ACCESS_KEY', ''), // 'token' => '', // token is optional ] ], 'retention' => 30, 'level' => 'info', 'group_name' => env('CLOUDWATCH_LOG_GROUP', 'group-log'), 'stream_name' => env('CLOUDWATCH_LOG_STREAM', 'error-log'), ], ], ];
Basic PHP Usage
<?php use Aws\CloudWatchLogs\CloudWatchLogsClient; use Monolog\Logger; use Monolog\Level; use Monolog\Formatter\JsonFormatter; use Dhs\CloudWatchLogs\Handler\CloudWatch; $sdkParams = [ 'region' => 'eu-west-1', 'version' => 'latest', 'credentials' => [ 'key' => 'your AWS key', 'secret' => 'your AWS secret', 'token' => 'your AWS session token', // token is optional ] ]; // Instantiate AWS SDK CloudWatch Logs Client $client = new CloudWatchLogsClient($sdkParams); // Log group name, will be created if none $groupName = 'php-logtest'; // Log stream name, will be created if none $streamName = 'ec2-instance-1'; // Days to keep logs, 14 by default. Set to `null` to allow indefinite retention. $retentionDays = 30; // Instantiate handler (tags are optional) $handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, ['my-awesome-tag' => 'tag-value'], Level::Info); // Optionally set the JsonFormatter to be able to access your log messages in a structured way $handler->setFormatter(new JsonFormatter()); // Create a log channel $log = new Logger('name'); // Set handler $log->pushHandler($handler); // Add records to the log $log->debug('Foo'); $log->warning('Bar'); $log->error('Baz');
Frameworks integration
AWS IAM needed permissions
If you prefer to use a separate programmatic IAM user (recommended) or want to define a policy, make sure following permissions are included:
CreateLogGroupaws docsCreateLogStreamaws docsPutLogEventsaws docsPutRetentionPolicyaws docsDescribeLogStreamsaws docsDescribeLogGroupsaws docs
When setting the $createGroup argument to false, permissions DescribeLogGroups and CreateLogGroup can be omitted
Sample 1: Write to any log stream in a log group
This policy example allows writing to any log stream in a log group (named my-app). The log streams will be created automatically.
Note: The first statement allows creation of log groups, and is not required when setting the $createGroup argument to false.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:DescribeLogGroups"
],
"Resource": "arn:aws:logs:*:*:log-group:*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:PutRetentionPolicy",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:log-group:my-app:*"
}
]
}
Sample 2: Write to specific log streams in a log group
This policy example allows writing to specific log streams (named my-stream-1 and my-stream-2) in a log group (named my-app). The log streams will be created automatically.
Note: The first statement allows creation of log groups, and is not required when setting the $createGroup argument to false.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:DescribeLogGroups"
],
"Resource": "arn:aws:logs:*:*:log-group:*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:PutRetentionPolicy"
],
"Resource": "arn:aws:logs:*:*:log-group:my-app:*"
},
{
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:*:*:log-group:my-app:log-stream:my-stream-1",
"arn:aws:logs:*:*:log-group:my-app:log-stream:my-stream-2",
]
}
]
}
dhs/cloudwatch-laravel-log 适用场景与选型建议
dhs/cloudwatch-laravel-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.84k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「monolog」 「aws」 「cloudwatch」 「laravel 10 cloudwatch log」 「laravel cloudwatch log」 「laravel 9 cloudwatch log」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dhs/cloudwatch-laravel-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dhs/cloudwatch-laravel-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dhs/cloudwatch-laravel-log 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Elastic Driver for Laravel Scout
Log events to AWS CloudWatch including embedded metrics
A Zend Framework module that sets up Monolog for logging in applications.
Laravel 5.6 Monolog custom telegram channel
统计信息
- 总下载量: 1.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-16