承接 elleryleung/custom-cloudwatch-logger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

elleryleung/custom-cloudwatch-logger

Composer 安装命令:

composer require elleryleung/custom-cloudwatch-logger

包简介

Write error log (both in string, array and Exception format) to AWS Cloudwatch.

README 文档

README

What is it for:

This package is to solve a single problem: to write error log (both in string, array and Exception) format to AWS Cloudwatch.

The variables will be json_encode() into string value and put it to Cloudwatch.

Tested environment

  • I am testing it using Docker PHP 8.2.

Composer install

composer require elleryleung/custom-cloudwatch-logger:dev-main

Notes

  • I haven't put it on composer Packagist, you would not find it there.
    • Updated: I put it on Packagist
  • Make sure you have necessary permission to create loggroup and logstream.
  • If the code cannot create either loggroup or logstream, a tradition error_log function is used to log this event
  • By default the log group name is PHPApplicationErrors and log stream name in the log group is ErrorLogStream.
  • If you read the source code CloudWatchErrorLogger.php, you will see that I put the whole $awsConfig inside the constructor.
    • Which means, if you want to add other configuration, add it in $awsConfig

How to create custom log group name and log stream name

Here you are:

$my_settigs = [
    'logGroupName' => 'test-log-group',
    'logStreamName' => 'test-log-stream',
];
$cloudWatchErrorLogger = new CloudWatchErrorLogger($awsConfig, $my_settigs);

$my_settigs is an associative array. At present the code only check below two array keys (The name implies itself, I would not explain it):

  • logGroupName
  • logStreamName

Known(?) issue(s)

  • I haven't tested using AWS cert or other crediential setting. I love to use key and secret because it is simple and work.

Example - Default

<?php
// Assume this is the test PHP code in your test project that uses the CustomCloudWatchLogger package

// Require the Composer autoloader to autoload the CustomCloudWatchLogger package and its dependencies
require 'vendor/autoload.php';

// Import the CloudWatchErrorLogger class from the CustomCloudWatchLogger package
use Elleryleung\CustomCloudwatchLogger\CloudWatchErrorLogger;

// Create AWS config for the CloudWatchErrorLogger
$awsConfig = [
    'region' => 'ap-east-1',
    'version' => 'latest',
    'credentials' => [
        'key' => 'your_aws_key',
        'secret' => 'your_aws_secret',
    ],
];

// Create an instance of the CloudWatchErrorLogger
$cloudWatchErrorLogger = new CloudWatchErrorLogger($awsConfig);

// Log a sample error to CloudWatch
$errorData = [
    'errno' => 123,
    'errstr' => 'Sample error message',
    'errfile' => 'sample.php',
    'errline' => 12,
];
$cloudWatchErrorLogger->logErrorToCloudWatch($errorData);

// Log a sample exception to CloudWatch
try {
    // This could be any code that may throw an exception
    throw new Exception('Sample exception message');
} catch (Exception $e) {
    $exceptionData = [
        'message' => $e->getMessage(),
        'file' => $e->getFile(),
        'line' => $e->getLine(),
    ];
    $cloudWatchErrorLogger->logErrorToCloudWatch($exceptionData);
}

Use default logGroupName and logStreamName

AWS Cloudwatch result

Example - Custom logGroupName and logStreamName

$custom = [
    'logGroupName' => 'test-log-group',
    'logStreamName' => 'test-log-stream',
];
$cloudWatchErrorLogger = new CloudWatchErrorLogger($awsConfig, $custom);

// Log a sample error to CloudWatch
$errorData = [
    'logGroupName' => $custom['logGroupName'],
    'logStreamName' => $custom['logStreamName'],
    'errno' => 123,
    'errstr' => 'Sample error message',
    'errfile' => 'sample.php',
    'errline' => 12,
];
$cloudWatchErrorLogger->logErrorToCloudWatch($errorData);

// Log a sample exception to CloudWatch
try {
    // This could be any code that may throw an exception
    throw new Exception('Sample exception message');
} catch (Exception $e) {
    $exceptionData = [
        'logGroupName' => $custom['logGroupName'],
        'logStreamName' => $custom['logStreamName'],
        'message' => $e->getMessage(),
        'file' => $e->getFile(),
        'line' => $e->getLine(),
    ];
    $cloudWatchErrorLogger->logErrorToCloudWatch($exceptionData);
}

Use custom logGroupName and logStreamName

AWS Cloudwatch result using custom name

Special thanks

  • Codeium: 80% or more of the code is generated by AI (Codeium) in VSCode. 20% of the code is added by me.

The AI Prompt I use

  • In case you are interetsed in what prompt I am using. Here you are
  • This is not the perfect prompt. There is no such thing as perfect prompt, which means if you found that it can be improved, yes you are right.

First prompt

Role: you are an experience PHP developer. You develop PHP application with efficient and clean code and produce result. Task: create a class that log all PHP error to AWS Cloudwatch Log when user calls error_log() function. Context: There is a need to create a standalone function that, when user uses your application and, when they need to log their message, like error_log(<string>), error_log(<array>), error_log(<object>), user can view the exact logged content in AWS Cloudwatchlog easily. You can reference to the following URL to get start:

Second prompt

Role: you are an experience PHP developer who are also an expert in AWS services. You develop PHP application with efficient and clean code and produce result. Task: Update the CloudWatchErrorLogger.php file and add checking that if loggroup and logstream does not exist, create them. Context: We use CloudWatchErrorLogger.php to log errors to CloudWatch, yet we need to check if loggroup and logstream exists or not. If not, create them. You can reference to the following URL to get start:

Here is the CloudWatchErrorLogger.php file:

... the code .....

elleryleung/custom-cloudwatch-logger 适用场景与选型建议

elleryleung/custom-cloudwatch-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 elleryleung/custom-cloudwatch-logger 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-12