maxbanton/cwh 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maxbanton/cwh

Composer 安装命令:

composer require maxbanton/cwh

包简介

AWS CloudWatch Handler for Monolog library

README 文档

README

Stand With Ukraine

AWS CloudWatch Logs Handler for Monolog

CI Coverage Status License Version Downloads

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.

Disclaimer

Logs are shipped to AWS CloudWatch Logs synchronously inside your PHP process. For very high-throughput workloads (thousands of records/sec sustained per process), consider an out-of-process pipeline — for example, logging to stdout and forwarding with the CloudWatch agent — that decouples log shipping from request latency and isolates AWS API failures from your app. For typical web and worker workloads, this handler is fine.

Requirements

  • PHP ^8.1
  • Monolog ^3.0
  • AWS account with proper permissions (see list of permissions below)

Features

  • Batches up to 10,000 events per PutLogEvents call (the AWS per-batch maximum), with automatic flush on size (1 MB), event-timespan (24 h), or buffer-full thresholds
  • Splits oversized records (> 1 MB) into multiple events automatically
  • Optional log-group creation with tags and retention policy
  • Lazy log-group / log-stream initialization on first write
  • Public flush() and reset() for long-lived workers (Symfony Messenger, Laravel queues / Octane, custom daemons)
  • Suitable for web applications and for long-running CLI daemons and workers

Installation

Install the latest version with Composer by running

$ composer require maxbanton/cwh:^3.0

Basic Usage

<?php

use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Maxbanton\Cwh\Handler\CloudWatch;
use Monolog\Formatter\JsonFormatter;
use Monolog\Level;
use Monolog\Logger;

$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::Debug);

// Set $createStream to false (10th argument) when the log stream is provisioned out of band
// (e.g. via Terraform `aws_cloudwatch_log_stream`). This skips DescribeLogStreams + CreateLogStream
// and lets you drop the matching IAM permissions.
// $handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, 10000, [], Level::Debug, true, true, false);

// 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');

Using IAM Task Roles on ECS / EC2

When running on ECS or EC2, prefer the task/instance IAM role over hard-coded credentials. Wrap the credential provider in memoize() so long-running workers don't hit metadata-endpoint timeouts:

<?php

use Aws\CloudWatchLogs\CloudWatchLogsClient;
use Aws\Credentials\CredentialProvider;

$provider = CredentialProvider::memoize(
    CredentialProvider::ecsCredentials()        // or ::instanceProfile() on EC2
);

$client = new CloudWatchLogsClient([
    'region'      => 'eu-west-1',
    'version'     => 'latest',
    'credentials' => $provider,
]);

Frameworks integration

And many others

Migrating from 2.x to 3.x

3.x is a breaking release that drops Monolog 2 and PHP 7.x support. The constructor parameter order, names, and defaults are preserved — existing Symfony YAML and Laravel with/handler_with configs continue to work after the platform upgrades below.

Required changes:

  • Bump PHP to 8.1 or newer.
  • Bump Monolog to 3.x.
  • The Monolog\Logger::DEBUG (and other) constants were removed in Monolog 3. Use Monolog\Level::Debug instead — int|string|Level are all accepted by the $level constructor argument.
  • Symfony service definitions referencing !php/const Monolog\Logger::WARNING must change to !php/const Monolog\Level::Warning.
  • Laravel channels using 'level' => 'warning' (string form) keep working unchanged.
  • Subclasses overriding protected function write(array $record): void must update the signature to protected function write(\Monolog\LogRecord $record): void and access fields via $record->property instead of $record['key'].

New (optional):

  • A 10th constructor argument $createStream (default true). Set to false to skip DescribeLogStreams/CreateLogStream for pre-provisioned streams; lets you drop those IAM permissions.

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:

  1. CreateLogGroup aws docs
  2. CreateLogStream aws docs
  3. PutLogEvents aws docs
  4. PutRetentionPolicy aws docs
  5. DescribeLogStreams aws docs
  6. DescribeLogGroups aws docs

When setting the $createGroup argument to false, permissions DescribeLogGroups and CreateLogGroup can be omitted.

When setting the $createStream argument to false, permissions DescribeLogStreams and CreateLogStream can be omitted. Use this when the log stream is provisioned out of band (e.g. via Terraform). If the stream does not exist at runtime, PutLogEvents will fail with ResourceNotFoundException.

AWS IAM Policy full json example

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:DescribeLogGroups"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:DescribeLogStreams",
                "logs:PutRetentionPolicy"
            ],
            "Resource": "{LOG_GROUP_ARN}"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:PutLogEvents"
            ],
            "Resource": [
                "{LOG_STREAM_1_ARN}",
                "{LOG_STREAM_2_ARN}"
            ]
        }
    ]
}

Issues

Feel free to report any issues

Contributing

Please check this document

Made in Ukraine 🇺🇦

maxbanton/cwh 适用场景与选型建议

maxbanton/cwh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.02M 次下载、GitHub Stars 达 425, 最近一次更新时间为 2016 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「monolog」 「aws」 「cloudwatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maxbanton/cwh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maxbanton/cwh 我们能提供哪些服务?
定制开发 / 二次开发

基于 maxbanton/cwh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.02M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 427
  • 点击次数: 27
  • 依赖项目数: 28
  • 推荐数: 0

GitHub 信息

  • Stars: 425
  • Watchers: 8
  • Forks: 90
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-03