pagevamp / laravel-cloudwatch-logs

Cloud Watch Driver for Laravel as needed at pagevamp
92 stars 48 forks source link

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

Closed yossefmydev closed 3 years ago

yossefmydev commented 4 years ago

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

developernaren commented 4 years ago

@yossefmydev I have created a PR to use this as a custom driver.

Can you test this out for me?

install as:

composer require pagevamp/laravel-cloudwatch-logs:dev-use-as-custom-driver

and this in your config if you have not

'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'),
            'disabled' => env('DISABLE_CLOUDWATCH_LOG', false),
            'formatter' => function ($configs) {
                return new \Monolog\Formatter\LineFormatter(
                    '%channel%: %level_name%: %message% %context% %extra%',
                    null,
                    false,
                    true
                );
            },
            'via' => \Pagevamp\Logger::class //remember this is the the class
        ],

additional param via is needed.

For this to work you will have to change you default driver as well, so in you .env add

LOG_CHANNEL=cloudwatch

edamk commented 4 years ago

@developernaren it works, but now it completely replaces default logging. An option to keep the default logging together with Cloudwatch logs would be ideal IMO.

netm commented 4 years ago

Hi there, I'm using Laravel 7 and trying to get this working as well. I did as above but can not yet see any logs in Cloudwatch. Your code is executing and not throwing any errors though. Any ideas on how I can debug?

It's easy enough to get normal logging working if you just add channels to the existing 'stack' channel.

        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily','cloudwatch'],
            'ignore_exceptions' => false,
        ],
developernaren commented 4 years ago

@netm It was working for me. If the log is not going it is most likely issue with AWS credentials. TBH, I tried as and independent channel and as Log::channel('cloudwatch') both worked for me but have not tried as a stack. But it should work. I will try as a stack when I have time and will let you know if I find anything.

rineez-livares commented 3 years ago

@yossefmydev I have created a PR to use this as a custom driver.

Can you test this out for me?

install as:

composer require pagevamp/laravel-cloudwatch-logs:dev-use-as-custom-driver

and this in your config if you have not

'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'),
            'disabled' => env('DISABLE_CLOUDWATCH_LOG', false),
            'formatter' => function ($configs) {
                return new \Monolog\Formatter\LineFormatter(
                    '%channel%: %level_name%: %message% %context% %extra%',
                    null,
                    false,
                    true
                );
            },
            'via' => \Pagevamp\Logger::class //remember this is the the class
        ],

additional param via is needed.

For this to work you will have to change you default driver as well, so in you .env add

LOG_CHANNEL=cloudwatch Hi @developernaren ,

  1. Why using line formatter here? Any advantages if we use line formatter instead of JsonFormatter?
  2. Is there any plan to merge the associated PR #24 ? It appears open for several months now.
developernaren commented 3 years ago

@rineez-livares

  1. This is a config you can easily change. When I first needed this repo it was because one of the example I used was not working. I just used the code from that file and just made it work, it did what I expected. Because I searched and did not find a working code / package for the cloudwatch, I made it a package hoping this would be helpful to others. I did not even write tests. It was a PR from someone else that I merged. Same with the formatter, it worked for my case, so just defaulted. If you think another format works better, just send a PR with arguments why it should be default, if it makes sense, I will merge.

  2. this did not seem to be a priority for most so just left as is and also because this is a major refactor and the way it works, I will have to do some additional testing for this but I am very busy at the moment. I may have some time after couple of weeks. I can test and release after that.

rineez-livares commented 3 years ago

It is definitely helpful thanks. I asked that question because the Readme on main branch shows an example that uses JsonFormatter. I was guessing that JsonFormatter is needed because cloudwatch requires logs to be submitted in json format. So I was curious when I saw your example config. I'm trying to use this branch in my app. You are right. It is probably not a good idea to merge this PR as an update to the existing main branch as it will break existing apps using this library as it require changes in configuration. But I feel integrating as a new driver is the better way to do cloudwatch integration for Laravel. May be this can be released as a new major version release.

developernaren commented 3 years ago

released a new version of this to work as regular channel. https://github.com/pagevamp/laravel-cloudwatch-logs/releases/tag/v1.0.1 closing this issue.