PHPackages                             elleryleung/custom-cloudwatch-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. elleryleung/custom-cloudwatch-logger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

elleryleung/custom-cloudwatch-logger
====================================

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

01PHP

Since Mar 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/alucard001/Custom-CloudWatch-Logger)[ Packagist](https://packagist.org/packages/elleryleung/custom-cloudwatch-logger)[ RSS](/packages/elleryleung-custom-cloudwatch-logger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Custom CloudWatch Logger
========================

[](#custom-cloudwatch-logger)

- [Custom CloudWatch Logger](#custom-cloudwatch-logger)
    - [What is it for:](#what-is-it-for)
    - [Tested environment](#tested-environment)
    - [Composer install](#composer-install)
    - [Notes](#notes)
    - [How to create custom log group name and log stream name](#how-to-create-custom-log-group-name-and-log-stream-name)
    - [Known(?) issue(s)](#known-issues)
    - [Example - Default](#example---default)
        - [Use default `logGroupName` and `logStreamName`](#use-default-loggroupname-and-logstreamname)
    - [Example - Custom `logGroupName` and `logStreamName`](#example---custom-loggroupname-and-logstreamname)
        - [Use custom `logGroupName` and `logStreamName`](#use-custom-loggroupname-and-logstreamname)
    - [Special thanks](#special-thanks)
        - [The AI Prompt I use](#the-ai-prompt-i-use)
            - [First prompt](#first-prompt)
            - [Second prompt](#second-prompt)

What is it for:
---------------

[](#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
------------------

[](#tested-environment)

- I am testing it using Docker PHP 8.2.

Composer install
----------------

[](#composer-install)

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

Notes
-----

[](#notes)

- I haven't put it on composer [Packagist](https://packagist.org/), 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
-------------------------------------------------------

[](#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)
-----------------

[](#known-issues)

- 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
-----------------

[](#example---default)

```
