elleryleung/custom-cloudwatch-logger

将错误日志(包括字符串、数组和Exception格式)写入AWS Cloudwatch。

dev-main 2024-03-12 09:49 UTC

This package is auto-updated.

Last update: 2024-09-12 11:04:08 UTC


README

它是用来做什么的

此包旨在解决一个问题:将错误日志(包括字符串、数组和Exception格式)写入AWS Cloudwatch。

变量将被json_encode()转换为字符串并放入Cloudwatch。

测试环境

  • 我正在使用Docker PHP 8.2进行测试。

Composer安装

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

注意

  • 我还没有将它放到composer Packagist上,所以你不会在那里找到它。
    • 更新:我已经将它放到Packagist上了
  • 确保你有创建日志组和日志流的必要权限。
  • 如果代码无法创建日志组或日志流,将使用传统的error_log函数记录此事件
  • 默认的日志组名称是PHPApplicationErrors,日志组中的日志流名称是ErrorLogStream
  • 如果你阅读了源代码CloudWatchErrorLogger.php,你会看到我把整个$awsConfig放在构造函数中。
    • 这意味着,如果你想添加其他配置,请添加到$awsConfig

如何创建自定义日志组名称和日志流名称

这里就是

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

$my_settings是一个关联数组。目前代码只检查以下两个数组键(名字本身就说明了,我就不解释了)

  • logGroupName
  • logStreamName

已知(?)问题

  • 我没有测试使用AWS证书或其他凭证设置。我喜欢使用keysecret,因为它简单且有效。

示例 - 默认

<?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);
}

使用默认的logGroupNamelogStreamName

AWS Cloudwatch result

示例 - 自定义logGroupNamelogStreamName

$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);
}

使用自定义的logGroupNamelogStreamName

AWS Cloudwatch result using custom name

特别感谢

  • Codeium:80%或以上的代码由AI(Codeium)在VSCode中生成。20%的代码是我添加的。

我使用的AI提示

  • 如果你对我的提示感兴趣,这里就是
  • 这不是一个完美的提示。没有什么是完美的提示,这意味着如果你发现它可以改进,那你是正确的。

第一次提示

角色:你是一位经验丰富的PHP开发者。你使用高效和干净的代码开发PHP应用程序并产生结果。任务:创建一个类,当用户调用error_log()函数时,将所有PHP错误记录到AWS Cloudwatch Log。上下文:需要创建一个独立的函数,当用户使用你的应用程序并需要记录他们的消息时,例如error_log(<string>)error_log(<array>)error_log(<object>),用户可以轻松地查看在AWS Cloudwatchlog中记录的确切内容。你可以参考以下URL开始

第二次提示

角色:您是一位经验丰富的PHP开发者,同时也是AWS服务的专家。您使用高效且简洁的代码开发PHP应用程序并产生结果。任务:更新CloudWatchErrorLogger.php文件,并添加检查以确保如果日志组(loggroup)和日志流(logstream)不存在,则创建它们。背景:我们使用CloudWatchErrorLogger.php将错误记录到CloudWatch,但我们需要检查日志组和日志流是否存在。如果不存在,则创建它们。您可以参考以下URL开始操作

以下是CloudWatchErrorLogger.php文件

... the code .....