Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 8 to 10 upgrade cloudwatch error #43

Closed
dekts opened this issue Aug 14, 2023 · 14 comments
Closed

Laravel 8 to 10 upgrade cloudwatch error #43

dekts opened this issue Aug 14, 2023 · 14 comments

Comments

@dekts
Copy link

dekts commented Aug 14, 2023

Declaration of Maxbanton\Cwh\Handler\CloudWatch::write(array $record): void must be compatible with Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): void

Screenshot 2023-08-14 at 9 44 33 AM
@jjozwiak
Copy link

@dekts Have you found a fix for this?

@jjozwiak
Copy link

Actual issue - maxbanton/cwh#116

Looks like this project has been abandoned

@thiagoflessak
Copy link

thiagoflessak commented Aug 23, 2023

As a workaround, until this package depends on maxbanton/cwh, it's possible to use this fork (phpnexus/cwh), instantiating the handler through service container - keeping the same config structure.

Remove pagevamp/laravel-cloudwatch-logs and install phpnexus/cwh.

Providers/AppServiceProvider.php

use Aws\CloudWatchLogs\CloudWatchLogsClient;
use PhpNexus\Cwh\Handler\CloudWatch;

// ...

public function register(): void
{
    // ...

    $this->app->singleton(CloudWatch::class, function () {
        $sdkParams = [
            'region' => config('logging.channels.cloudwatch.region'),
            'version' => config('logging.channels.cloudwatch.version'),
        ];

        if (config('logging.channels.cloudwatch.credentials.key')) {
            $sdkParams['credentials'] = [
                'key' => config('logging.channels.cloudwatch.credentials.key'),
                'secret' => config('logging.channels.cloudwatch.credentials.secret'),
            ];
        }

        return new CloudWatch(
            new CloudWatchLogsClient($sdkParams),
            config('logging.channels.cloudwatch.group_name'),
            config('logging.channels.cloudwatch.stream_name'),
            config('logging.channels.cloudwatch.retention'),
            config('logging.channels.cloudwatch.batch_size')
        );
    });
}

config/logging.php

use PhpNexus\Cwh\Handler\CloudWatch;

// ...

        'cloudwatch' => [
            'driver' => 'monolog',
            'handler' => CloudWatch::class,
            //'via' => \Pagevamp\Logger::class, // <-- remove or comment
        ]

@developernaren
Copy link
Contributor

I no longer use this package, so have not really tested this.
there is an open PR #42, if you guys can confirm that this works, I will merge this PR and create a new version of this.

@Renato-Silva
Copy link

The open PR solves it

@hungnv-sr
Copy link
Contributor

hungnv-sr commented Aug 29, 2023

I no longer use this package

@developernaren can I ask what are you using instead?

@developernaren
Copy link
Contributor

@hungnv-sr I no longer use PHP in my job. I still love PHP though.

Also, I have released a new version v1.1.0 .

@jmsutton1981
Copy link

jmsutton1981 commented Aug 29, 2023

I've upgraded but I'm getting the following error hitting my logs in Laravel...

laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (Error(code: 0): Class "Phpnexus\Cwh\Handler\CloudWatch" not found at /var/www/vendor/pagevamp/laravel-cloudwatch-logs/src/Logger.php:34)

Have I missed something somewhere?

@hungnv-sr
Copy link
Contributor

@jmsutton1981 it was my bad when creating pull request.
Updated: #44

@developernaren I'm sorry, can you merge that again?

@developernaren
Copy link
Contributor

@hungnv-sr has sent a PR to fix the typo in the name of the imported package. I have merged and created a new version. v1.1.1
@jmsutton1981 please check.
thanks for the contribution guys!

@jmsutton1981
Copy link

Updated and this has fixed the issue, thanks everyone

@developernaren
Copy link
Contributor

closing this as looks like this is fixed!

@gregclarity
Copy link

I think there still might be something going on here.

We pushed updates to all of our deployments yesterday and all of a sudden cloudwatch logging quit working. No errors, nothing amiss on the frontend, just logs stopped showing up in CW.

I went looking and found this ticket, then did a composer update... I pulled the latest version of this dependency 1.1.1, and verified the fixes outlined here are in my vendor dir...

I also tried the workaround written above and still no logs. Any help would be greatly appreciated on this as we've got some production clients running on this.

@dekts
Copy link
Author

dekts commented Sep 21, 2023

@dekts Have you found a fix for this?

Yes, I built custom PHP file to log using phpnexus/cwh

Install first this phpnexus/cwh package in your project.

Then I have added one file inside the App\Logging directory called CloudWatchLoggerFactory.php

This is the code of CloudWatchLoggerFactory.php

<?php

namespace App\Logging;

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

class CloudWatchLoggerFactory
{
    /**
     * Create a custom Monolog instance.
     *
     * @param  array  $config
     * @return \Monolog\Logger
     */
    public function __invoke(array $config)
    {
        $sdkParams = $config["sdk"];
        $tags = $config["tags"] ?? [];
        $name = $config["name"] ?? 'cloudwatch';

        // Instantiate AWS SDK CloudWatch Logs Client
        $client = new CloudWatchLogsClient($sdkParams);

        // Log group name, will be created if none
        $groupName = $config["group_name"];

        // Log stream name, will be created if none
        $streamName = $config["stream_name"];

        // Days to keep logs, 14 by default. Set to `null` to allow indefinite retention.
        $retentionDays = $config["retention"];

        $batchSize = $config["batch_size"];

        // Instantiate handler (tags are optional)
        $handler = new CloudWatch($client, $groupName, $streamName, $retentionDays, $batchSize, $tags);

        // Optionally set the JsonFormatter to be able to access your log messages in a structured way
        $handler->setFormatter(new JsonFormatter());

        // Create a log channel
        $logger = new Logger($name);

        // Set handler
        $logger->pushHandler($handler);

        return $logger;
    }
}

Then change the logging.php config like below:

'cloudwatch' => [
    'driver' => 'custom',
    'name' => env('CLOUDWATCH_LOG_NAME', ''),
    'sdk' => [
        'region' => env('CLOUDWATCH_LOG_REGION', 'us-east-1'),
        'version' => 'latest',
       'credentials' => [
            'key' => env('CLOUDWATCH_LOG_KEY'),
            'secret' => env('CLOUDWATCH_LOG_SECRET')
       ]
    ],
    'stream_name' => env('CLOUDWATCH_LOG_STREAM_NAME', 'laravel_app'),
    'retention' => env('CLOUDWATCH_LOG_RETENTION_DAYS', 7),
    'group_name' => env('CLOUDWATCH_LOG_GROUP_NAME', 'laravel_app'),
    'version' => env('CLOUDWATCH_LOG_VERSION', 'latest'),
    'formatter' => \Monolog\Formatter\JsonFormatter::class,
    'batch_size' => env('CLOUDWATCH_LOG_BATCH_SIZE', 10000),
    'level' => env('CLOUDWATCH_LOG_LEVEL', 'debug'),
    'via' => \App\Logging\CloudWatchLoggerFactory::class,
],

Here we just need to change via attribute value. from \Pagevamp\Logger::class to \App\Logging\CloudWatchLoggerFactory::class,.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants