maknz/slack

Sending repeats infinitely

Closed this issue · 7 comments

Interesting things happening if you write something like this in your errors Handler class:

public function report(Exception $e)
    {
        if (app()->environment() == 'production') {
            Slack::send('*' . \Request::url() . '* ' . (string)$e);
        }

        return parent::report($e);
    }

it starts repeats infinitely. How can I investigate if it's package or laravel or forge problem?

maknz commented

I'm pretty sure there's no problem with the package as it's quite widely used and this has never been reported.

Replace Slack::send with some kind of logging and see what happens. If you get multiple logs like you did multiple Slack messages, it'll be your code.

I think that there is any missconfiguration of the Slack-Package and it repeats cause it causes another exception.
We are using this package also for logs but in combination with the Monolog Class and it works like a charm.

Our code

bootstrap/app.php

$app->configureMonologUsing(function (Monolog\Logger $monolog) {
    $slackHandler = new \App\Libs\Logging\SlackHandler();
    $monolog->pushHandler($slackHandler);

    $monolog->pushProcessor(new \Monolog\Processor\WebProcessor($_SERVER));
    $monolog->pushProcessor(function ($record) {
        $record['extra']['session_id'] = Cookie::get(Config::get('session.cookie'));
        return $record;
    });
});

app/Libs/Logging/SlackHandler.php

<?php
namespace Cali\Libs\Logging;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

class SlackHandler extends AbstractProcessingHandler
{
    public function __construct($level = Logger::WARNING, $bubble = true)
    {
        parent::__construct($level, $bubble);
    }

    protected function write(array $record)
    {
        \Slack::to(config('slack.channel'))->withIcon(':beetle:')->attach([
            'fallback' => $record['formatted'],
            'color' => 'danger',
            'fields' => [
                [
                    'title' => 'Server',
                    'value' => array_get($record, 'server', env('APP_ENV', 'cali')),
                    'short' => true
                ],
                [
                    'title' => 'Level',
                    'value' => array_get($record, 'level_name', 'WARNING'),
                    'short' => true
                ],
                [
                    'title' => 'URL',
                    'value' => array_get($record, 'extra.url'),
                    'short' => true
                ],
                [
                    'title' => 'Request-IP',
                    'value' => array_get($record, 'extra.ip'),
                    'short' => true
                ],
            ],
        ])->send(explode("\n", array_get($record, 'message'))[0]);
    }
}

This adds another Monolog handler, in the Exception-Handler itself you haven't to change anything and it logs all Logs with a Loglevel over WARNING to slack.

maknz commented

Good point. @justutiz, try your Slack::send statement outside of the error handling (in a regular controller). You might be getting an exception.

You know, intresting things happening:

Yoto3 Production BOT [9:27 AM] 
*https://yoto3.com/build/js/all-28ca59126b.js* Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/forge/yoto3.com/bootstrap/cache/compiled.php:8520
[9:28] 
*https://yoto3.com/build/js/all-28ca59126b.js** Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/forge/yoto3.com/bootstrap/cache/compiled.php:8520
9:28] 
*https://yoto3.com/build/js/all-28ca59126b.js*** Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/forge/yoto3.com/bootstrap/cache/compiled.php:8520

look at the * in the end ;) I wait couple seconds and the last one is

9:29] 
*https://yoto3.com/build/js/all-28ca59126b.js**************************************************************************************************************** Symfony\Component\HttpKernel\Exception\NotFoundHttpException in /home/forge/yoto3.com/bootstrap/cache/compiled.php:8520

I think it's proves that it is maybe package error? No? :) I get this error only in this case

maknz commented

Not a lot to go on, but the NotFoundHttpException would suggest that the Slack endpoint could be a problem. What do you have that set to be?

Slack has changes the channel param, it have to Start with a # now. It was our Problem last time that also causes a HttpException.

But also for the code structure and useage of existing things I will recommend you to extend the Monolog class that automaticly get's called foreach exception. And you can also send messages manually by calling: Log::error(...).

Monolog has an existing SlackHandler but it won't work for us, that's why we created our own.

And try to recompile the compiled.php and run a composer update to be sure that's everything fine.