Git Product home page Git Product logo

laravel-cloudwatch-logs's Introduction

Logger for Aws Cloud Watch

Breaking Change for version 1.0

When this package started, it started as a listener for log events and would only work with another channel. This package would listen to log events and just add extra log to cloud watch. So, you did not need to add cloudwatch as a channel. But after 1.0 it works as a custom driver. So, you MUST add LOG_CHANNEL as cloudwatch in your logging config for this to work going forward.

Installation

composer require pagevamp/laravel-cloudwatch-logs

Example

You can use laravel's default \Log class to use this

\Log::info('user logged in', ['id' => 123, 'name' => 'Naren']);

Usage with AWS Lambda

Make sure the AWS Lambda template contains an IAM role with enough access. So think about Logs:CreateLogGroup, Logs:DescribeLogGroups, Logs:CreateLogStream, Logs:DescribeLogStream, Logs:PutRetentionPolicy and Logs:PutLogEvents

Config

Config for logging is defined at config/logging.php. Add cloudwatch to the channels array

'channels' =>  [
    'cloudwatch' => [
            'driver' => 'custom',
            'name' => env('CLOUDWATCH_LOG_NAME', ''),
            'region' => env('CLOUDWATCH_LOG_REGION', ''),
            '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', 14),
            '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),    
            'via' => \Pagevamp\Logger::class,
        ],
]

And set the LOG_CHANNEL in your environment variable to cloudwatch.

If the role of your AWS EC2 instance has access to Cloudwatch logs, CLOUDWATCH_LOG_KEY and CLOUDWATCH_LOG_SECRET need not be defined in your .env file.

Contribution

I have added a pre-commit hook to run php-cs-fixer whenever you make a commit. To enable this run sh hooks.sh.

laravel-cloudwatch-logs's People

Contributors

developernaren avatar foxxmd avatar hungnv-sr avatar laravel-shift avatar luigel avatar martenvanurk avatar naveenk87 avatar niekvanderkooy avatar rmundel avatar sujansh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-cloudwatch-logs's Issues

Error in using logs within queued Jobs

In Laravel 5.7.19 (latest) this driver is not working at all within the Job class nor within the queued Listeners. Tried also with using

Log::channel(...)

and without channel. (ie Log::info...)

Error message from the default laravel.log file is:

laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (ErrorException(code: 0): Undefined index: driver at /some/path/Sites/demo/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:170)

Monolog 2.0 compatibility

With the release of v2.0.0 of maxbanton/cwh , that lib is now Monolog 2.0 compatible.

Would be awesome if that could be supported here as well :)

New version

Hi, could you release a new version to packagist, to include commit ed43b3a? Thanks!

Laravel 6 support?

Could you addd laravel 6 support pls?
Just exclude illuminate/support from requires - this should fix the issue

When trying to install on laravel 11, an error occurs

When trying to install on laravel 11, an error occurs

composer require pagevamp/laravel-cloudwatch-logs

โ†“

% composer require pagevamp/laravel-cloudwatch-logs
./composer.json has been updated
Running composer update pagevamp/laravel-cloudwatch-logs
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - pagevamp/laravel-cloudwatch-logs[v1.0.3, ..., v1.1.1] require illuminate/support ^5.1 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.16, v10.0.0, ..., v10.48.2] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs v1.0.2 requires illuminate/support ^5.1 || ^6.0 || ^7.0 || ^8.0 || ^9.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.16] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs[0.2.2, v1.0.1] require illuminate/support ^5.1 || ^6.0 || ^7.0 || ^8.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs[0.2.0, ..., 0.2.1] require illuminate/support ^5.1 || ^6.0 || ^7.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs 0.1.1 requires illuminate/support ^5.1 || ^6.0 -> found illuminate/support[v5.1.1, ..., v5.8.36, v6.0.0, ..., v6.20.44] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs[0.0.4, ..., 0.1] require illuminate/support ^5.1 -> found illuminate/support[v5.1.1, ..., v5.8.36] but these were not loaded, likely because it conflicts with another require.
    - pagevamp/laravel-cloudwatch-logs[0.0.1, ..., 0.0.3] require illuminate/support ^5.6 -> found illuminate/support[v5.6.0, ..., v5.8.36] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires pagevamp/laravel-cloudwatch-logs * -> satisfiable by pagevamp/laravel-cloudwatch-logs[0.0.1, ..., 0.2.2, v1.0.1, ..., v1.1.1].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require pagevamp/laravel-cloudwatch-logs:*" to figure out if any version is installable, or "composer require pagevamp/laravel-cloudwatch-logs:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Laravel 8 to 10 upgrade cloudwatch error

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

Not working from Tinker and Impossible to use the channel as a regular channel

Hi,

First, thanks for this package! It is working but I wanted to share 2 possible improvements:

  1. It's not working from Tinker
  2. I can't log to the cloudwatch channel, for example with: \Log::channel('cloudwatch')->info('test from Laravel App');. In this case, I received the following error:
{
    "message": "Unable to create configured logger. Using emergency logger.",
    "context": {
        "exception": {
            "class": "ErrorException",
            "message": "Undefined index: via",
            "code": 0,
            "file": "/home/pc-10/Projects/laravel7/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:227"
        }
    },
    "level": 600,
    "level_name": "EMERGENCY",
    "channel": "laravel7",
    "datetime": "2020-07-19T08:57:08.870730+00:00",
    "extra": {}
}

Thanks for your help

Cannot disable for local environment

I have added DISABLE_CLOUDWATCH_LOG=true to my .env file however it is returning NULL.

in boot() method of CloudWatchServiceProvider you are checking for
if (!env('DISABLE_CLOUDWATCH_LOG')) { ... }

According to the Laravel docs, this will never execute if the config file is cached. It is considered best practice to have a namespaced config file and test this value via config('cloudwatch.disable_cloudwatch_logs');

I would be more than happy to submit a PR for this.

Tested with Laravel 5.8

Add config option for $createGroup

Requested feature

Configuration option create_group

Rationale

When the underlying Maxbanton\Cwh\Handler\Cloudwatch instance flushes the logs, it will initialize itself and check if the log group and log stream already exist or create them if they do not.
To prevent this behavior (for example due to limited permissions on AWS), they provide the constructor parameter createGroup which defaults to true -> to source code. It would be beneficial to have this parameter exposed as config option of this log handler so it can be configured on the application level. Currently, the parameter is left unspecified in this library -> to source code

Let me know what you think about this so I might open a pull request in the future.

Unable to create configured logger. Using emergency logger.

I followed to readme.md and got error

 {
    "message": "Unable to create configured logger. Using emergency logger.",
    "context": {
        "exception": {
            "class": "ErrorException",
            "message": "Undefined index: driver",
            "code": 0,
            "file": "/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:184"
        }
    },
    "level": 600,
    "level_name": "EMERGENCY",
    "channel": "channel_1",
    "datetime": {
        "date": "2020-04-13 14:46:01.110488",
        "timezone_type": 3,
        "timezone": "UTC"
    },
    "extra": []
}

Version laravel 5.6 updated from laraval 5.5

No logs when using with Supervisor

I am using this package in a worker setup using Laravel. I run the listener using Supervior and the logging is working until the console command. When it calls jobs from the command, the process is running fine , but there is no logs after that.

I think there still might be something going on here.

          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.

Originally posted by @gregclarity in #43 (comment)

Symfony\Component\ErrorHandler\Error\FatalError: Declaration of Maxbanton\Cwh\Handler\CloudWatch::write(array $record): void must be compatible with Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): void in file /maxbanton/cwh/src/Handler/CloudWatch.php on line 164

"laravel/framework": "v10.19.0"
"pagevamp/laravel-cloudwatch-logs":"v1.1.1",
"monolog/monolog": "3.4.0",

I added in logging.php
'cloudwatch' => [
'driver' => 'custom',
'name' => env('CLOUDWATCH_LOG_NAME', ''),
'region' => env('CLOUDWATCH_LOG_REGION', ''),
'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', 14),
'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),
'via' => \Pagevamp\Logger::class,
],

The option 'via' causes to get the error when i run a simple log Symfony\Component\ErrorHandler\Error\FatalError: Declaration of Maxbanton\Cwh\Handler\CloudWatch::write(array $record): void must be compatible with Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): void in file /maxbanton/cwh/src/Handler/CloudWatch.php on line 164

I found that the monolog/monolog version need to be under 3.0.0 to make it works. But laravel/framework v10 requires monolog version to be upper than 3.0.

how can we solve this issue please?

log level

hey, noticed it is not possible to set level of errors for cloudwatch (custom) channel

global log level is OK, but specific environments require different levels to be dynamic

I did some code tweak in CloudWatchServiceProvider.php ::

//allow error+ only level
if($level=="error" || $level=="critical" || $level=="alert" || $level=="emergency") {
    if ($message instanceof \ErrorException) {
        return $this->getLogger()->log($level, $message, $context);
    }

    if ($app['cloudwatch.logger'] instanceof Logger) {
        $app['cloudwatch.logger']->log($level, $message, $context);
    }
}

but it would be nice to mimic level param as on rest of channels, so it is dynamically programmable

Import for use PhpNexus\Cwh\Handler\CloudWatch; broken

Locally I was having an issue with this library.

When trying out the Logger.php without this library along with the dependencies on cwh, I got the same import error.

After changing

use PhpNexus\Cwh\Handler\CloudWatch;

to

use Maxbanton\Cwh\Handler\CloudWatch;

It worked. Not sure if this is a bug or not but if it is it should be addressed

Batch Size Meaning

Hi,

Not an issue per say but wanted to find out if the package is syncronous in logging or asyncronous, and does batch_size key mean logs need to reach a certain size before being shipped to AWS?

"Class does not exist" when trying to log

I am trying to use this for Laravel 6. My logging config looks like this:

'cloudwatch' => [
    'driver' => 'custom',
    'name' => 'myname',
    'region' => 'us-east-1',
    'credentials' => [ // EC2 instance has IAM role with CloudWatch access
        'key' => '',
        'secret' => ''
    ],
    'stream_name' => 'mystream',
    'retention' => 7,
    'group_name' => 'mygroupname',
    'version' => 'latest',
    'formatter' => \Monolog\Formatter\JsonFormatter::class,
    'disabled' => false,
],

When I try to log in my application via Log::channel('cloudwatch')->debug('Testing'), I get the following error:

Unable to create configured logger. Using emergency logger. {"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target class [] does not exist. at laravel/framework/src/Illuminate/Container/Container.php:805)
[stacktrace]
...
[previous exception] [object] (ReflectionException(code: -1): Class  does not exist at laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:803)

I believe I followed all the proper configuration steps so I am confused with what is happening here.

Add batch_size parameter

Hi I suggest to add this and add on readme too

'batch_size' => env('CLOUDWATCH_LOG_BATCH_SIZE', 10000),

because the first time you test it you don't see events and on develop generate 10K events is hard

Thanks for this helpful package!

stack traces

Hey, great package.
As ive set this up ive noticed a couple of commits around the formatter, LineFormater and JsonFormatter.
Is there some way to include the stack traces. theres mention in Monolog/Formatters/JsonFormatter but not been able to set this via the logging config, any ideas?
Keep up the great work!

Updating to Laravel 7 Causes InvalidSequenceTokenException

Error executing "PutLogEvents" on "https://logs.us-east-1.amazonaws.com"; AWS HTTP error: Client error: POST https://logs.us-east-1.amazonaws.com resulted in a 400 Bad Request response:

{
  "__type": "InvalidSequenceTokenException",
  "expectedSequenceToken": "49597042187774269928909159510882100678280996470456157 (truncated...)"
}

InvalidSequenceTokenException (client): The given sequenceToken is invalid. The next expected sequenceToken is:
49597042187774269928909159510882100678280996470456157586

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.